@Async 和 @Transactional:不工作
Posted
技术标签:
【中文标题】@Async 和 @Transactional:不工作【英文标题】:@Async and @Transactional: not working 【发布时间】:2013-11-24 23:50:34 【问题描述】:请查看代码。
当我调用 @Async loadMarkUpPCT() 方法时,数据不会提交到表中。它表现得好像没有牵引力。
当我从 loadMarkUpPCT(1 类)中删除 @Async(即非异步)时,数据已按预期提交并正常:事务性)
我期望@Async 和@Transactional 会得到相同的结果,但事实并非如此。请解释或我做错了什么?
已编辑:我刚刚编辑发布代码+日志
流动方式: AppDataLoaderController 调用 AppDataLoaderService 调用 DataMigrationService 调用 JpaDataMigrationDao
package concepts.web.rest.resource.spring.impl;
@Controller
@RequestMapping("/appdataloader")
public class AppDataLoaderController
@RequestMapping("/loadMarkupPct")
@ResponseStatus(HttpStatus.ACCEPTED)
public void loadMarkUpPCT()
try
this.appDataLoaderService.loadMarkUpPCT();
catch (ServiceException e)
e.printStackTrace();
package concepts.service.impl;
@Service("appDataLoaderService")
public class AppDataLoaderServiceImpl implements AppDataLoaderService
@Async
@Override
public void loadMarkUpPCT() throws ServiceException
logger.debug("@Async loadMarkUpPCT");
dataMigrationService.loadMarkUpPCT();
package concepts.service.impl;
@Service
@Scope("prototype")
public class DataMigrationServiceImpl implements DataMigrationService
@Override
public void loadMarkUpPCT() throws ServiceException
// TODO Auto-generated method stub
Assert.notNull(markUpPCTDataLoader);
List<MarkUpPCT> markUpPCTs=markUpPCTDataLoader.getMarkupCoef();
for (MarkUpPCT markUpPCT: markUpPCTs)
dataMigrationDao.storeMarkUpPCT(markUpPCT);
package concepts.persistence.impl.jpa;
@Repository
public class JpaDataMigrationDao extends DataMigrationDaoAdapter
@PersistenceContext
private EntityManager entityManager;
@Transactional
@Override
public void storeMarkUpPCT(MarkUpPCT markUpPCT)
entityManager.persist(markUpPCT);
一些日志
14 Nov 2013 18:47:05,531 36813 [http-bio-18080-exec-3] DEBUG OpenEntityManagerInViewFilter - Opening JPA EntityManager in OpenEntityManagerInViewFilter
14 Nov 2013 18:47:05,578 36860 [http-bio-18080-exec-3] DEBUG DispatcherServlet - DispatcherServlet with name 'mvc' processing POST request for [/POCQI/appdataloader/loadMarkupPct]
[http-bio-18080-exec-3] DEBUG RequestMappingHandlerMapping - Looking up handler method for path /appdataloader/loadMarkupPct
[http-bio-18080-exec-3] DEBUG RequestMappingHandlerMapping - Returning handler method [public void concepts.web.rest.resource.spring.impl.AppDataLoaderController.loadMarkUpPCT()]
[SimpleAsyncTaskExecutor-1] DEBUG DataMigrationServiceImpl - @Async loadMarkUpPCT
[http-bio-18080-exec-3] DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc': assuming HandlerAdapter completed request handling
[SimpleAsyncTaskExecutor-1] DEBUG MarkUpPCTDataLoader - 80=1.6, 90=1.8, 100=2.0, 105=2.05, 110=2.1, 115=2.15, 117=2.17, 120=2.2, 125=2.25, 150=2.5
[http-bio-18080-exec-3] DEBUG DispatcherServlet - Successfully completed request
[http-bio-18080-exec-3] DEBUG OpenEntityManagerInViewFilter - Closing JPA EntityManager in OpenEntityManagerInViewFilter
[http-bio-18080-exec-3] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Opening JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Registering transaction synchronization for JPA EntityManager
[SimpleAsyncTaskExecutor-1] DEBUG EntityManagerFactoryUtils - Closing JPA EntityManager
【问题讨论】:
请发布@Async
和@Transactinal
的配置。
我用的是Spring 3.2 applicationContext-service.xml applicationContext-jpa.xml
请同时发布这些文件和您的课程所在的包。
流程:AppDataLoaderController 调用 AppDataLoaderService 调用 DataMigrationService 调用 JpaDataMigrationDao
Sotirios,我刚刚发布了代码+日志。
【参考方案1】:
尝试同时使用 @Transactional
注释 loadMarkUpPCT()
方法并告诉我们这是否有效。
【讨论】:
【参考方案2】:我刚刚遇到了同样的问题。原来我忘了添加@EnableAsync 注释。
【讨论】:
以上是关于@Async 和 @Transactional:不工作的主要内容,如果未能解决你的问题,请参考以下文章
Spring @Transactional 和 @Async
如何使用@Async 和@Transactional 在多线程中回滚?
从Transactional与Async同时使用的错误到动态代理
具有@async超时值的Spring @transactional不起作用