Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

spring - @Async and @Transactional: not working

Please see code.

  1. When I called the method @Async loadMarkUpPCT(), data is NOT committed into the table. It behaves as if it's non-tractional.

  2. When I removed @Async from loadMarkUpPCT (Class 1), i.e. non-async, then data is committed and OK as expected: transactional)

I was expecting to have the same result with @Async and @Transactional but it's NOT. Please explain or what did I do wrong?

Edited: I just edited to post the code + log

Flow-wise: AppDataLoaderController calls AppDataLoaderService calls DataMigrationService calls 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);

    }

Some logs

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to also annotate the loadMarkUpPCT() method with @Transactional and tell us if that worked.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...