..... <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="jpaDialect" ref="jpaDialect"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> ....
@Service public class FooServiceImpl implements FooService { @Autowired private FooDao fooDao; @Transactional @Override public void update(Foo entity) { fooDao.merge(entity); } }
@Service public class FooServiceImpl implements FooService { @Autowired private TaskExecutor taskExecutor; @Autowired private FooDao fooDao; @Transactional @Override public void update(Foo entity) { fooDao.merge(entity); taskExecutor.run(new Runnable() { public void run() { someLongTimeOperation(entity); } }); } @Transactional @Override public void someLongTimeOperation(Foo entity) { // } }
@Service public class FooServiceImpl implements FooService { @Autowired private TaskExecutor taskExecutor; @Autowired private FooDao fooDao; @Transactional @Override public void update(final Foo entity) { fooDao.merge(entity); final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); taskExecutor.execute(new Runnable() { @Override public void run() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { someLongTimeOperation(entity); } }); } }); } @Transactional @Override public void someLongTimeOperation(Foo entity) { // } }
public interface TransactionalAsyncTaskExecutor extends AsyncTaskExecutor { void execute(Runnable task, Integer propagation, Integer isolationLevel); }
public class DelegatedTransactionalAsyncTaskExecutor implements InitializingBean, TransactionalAsyncTaskExecutor { private PlatformTransactionManager transactionManager; private AsyncTaskExecutor delegate; private TransactionTemplate sharedTransactionTemplate; public DelegatedTransactionalAsyncTaskExecutor() { } public DelegatedTransactionalAsyncTaskExecutor(PlatformTransactionManager transactionManager, AsyncTaskExecutor delegate) { this.transactionManager = transactionManager; this.delegate = delegate; } @Override public void execute(final Runnable task, Integer propagation, Integer isolationLevel) { final TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); transactionTemplate.setPropagationBehavior(propagation); transactionTemplate.setIsolationLevel(isolationLevel); delegate.execute(new Runnable() { @Override public void run() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { task.run(); } }); } }); } @Override public void execute(final Runnable task) { execute(task, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT); } @Override public void execute(final Runnable task, long startTimeout) { final TransactionTemplate transactionTemplate = getSharedTransactionTemplate(); delegate.execute(new Runnable() { @Override public void run() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { task.run(); } }); } }, startTimeout); } @Override public Future<?> submit(final Runnable task) { final TransactionTemplate transactionTemplate = getSharedTransactionTemplate(); return delegate.submit(new Runnable() { @Override public void run() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { task.run(); } }); } }); } @Override public <T> Future<T> submit(final Callable<T> task) { final TransactionTemplate transactionTemplate = getSharedTransactionTemplate(); return delegate.submit(new Callable<T>() { @Override public T call() throws Exception { return transactionTemplate.execute(new TransactionCallback<T>() { @Override public T doInTransaction(TransactionStatus status) { T result = null; try { result = task.call(); } catch (Exception e) { e.printStackTrace(); status.setRollbackOnly(); } return result; } }); } }); } public PlatformTransactionManager getTransactionManager() { return transactionManager; } public void setTransactionManager(PlatformTransactionManager transactionManager) { this.transactionManager = transactionManager; } public AsyncTaskExecutor getDelegate() { return delegate; } public void setDelegate(AsyncTaskExecutor delegate) { this.delegate = delegate; } public TransactionTemplate getSharedTransactionTemplate() { return sharedTransactionTemplate; } public void setSharedTransactionTemplate(TransactionTemplate sharedTransactionTemplate) { this.sharedTransactionTemplate = sharedTransactionTemplate; } @Override public void afterPropertiesSet() { if (transactionManager == null) { throw new IllegalArgumentException("Property 'transactionManager' is required"); } if (delegate == null) { delegate = new SimpleAsyncTaskExecutor(); } if (sharedTransactionTemplate == null) { sharedTransactionTemplate = new TransactionTemplate(transactionManager); } } }
<bean id="transactionalTaskExecutor" class="ru.habrahabr.support.spring.DelegatedTransactionalAsyncTaskExecutor"> <property name="delegate"> <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="threadNamePrefix" value="Habrahabr example - "/> <property name="threadGroupName" value="Habrahabr examples Group"/> <property name="corePoolSize" value="10"/> <property name="waitForTasksToCompleteOnShutdown" value="true"/> </bean> </property> <property name="transactionManager" ref="transactionManager"/> </bean>
@Service public class FooServiceImpl implements FooService { @Autowired private TransactionalAsyncTaskExecutor trTaskExecutor; @Autowired private FooDao fooDao; @Transactional @Override public void update(Foo entity) { fooDao.merge(entity); // Spring' (tr_1). trTaskExecutor.run(new Runnable() { // (tr_2), run() tr_2. public void run() { someLongTimeOperation(); } }); } // tr_1 . tr_2 TransactionTemplate. @Transactional @Override public void someLongTimeOperation(Foo entity) { // } }
Source: https://habr.com/ru/post/228953/
All Articles