Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring+JPA @Transactional not committing

Tags:

spring

I understand the similar question have been asked before here, but I couldn't find the solution to my problem. Basically, I am trying to use JPA through Hibernate in Spring, but the data is not being persisted for some reason.Turning on debug on spring transaction reveals nothing - EntityManager open and closed, but nothing shows up as far as transaction manager concerns ... I am sure I miss something big, any help is appreciated! see the following for more details.

TIA

Oliver

The basic layout is as follows: class FooDaoJPA’s save function calls out entityManager.persist(object) to persist the object.

class FooServiceImpl implements the service interface by:

@Transactional(rollbackFor = DataAccessException.class,
                    readOnly = false, timeout = 30,
                    propagation = Propagation.SUPPORTS,
                    isolation = Isolation.DEFAULT)
 public void saveFoo(Foo foo) throws DataAccessException {
        fooDao.save(foo);
}

Noted that fooDao is injected by Spring IoC

Finally controller is injected a FooService and call saveFoo() to persist data.

JPA configuration

<!-- JPA Entity Manager Factory -->
<bean id="entityManagerFactory" 
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:dataSource-ref="feDataSource"/>

<!-- Transaction Config -->
<bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>

<tx:annotation-driven mode="aspectj"                        
transaction-manager="transactionManager"/>
like image 904
Oliver Avatar asked Mar 05 '11 03:03

Oliver


2 Answers

Note the mode="aspectj" in your configuration. It requires additional configuration and usually you shouldn't use it unless you understand what does it mean and why do you need it. See 10.5.6 Using @Transactional.

like image 188
axtavt Avatar answered Sep 29 '22 08:09

axtavt


The first thing that looks like a potential issue is your setting for propagation. Here is documentation showing the values you can specify:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/transaction/annotation/Propagation.html

Note that you've specified SUPPORTS which "Support a current transaction, execute non-transactionally if none exists". You probably want REQUIRED, which is the default, and will either use an existing transaction or create one if one does not currently exist.

like image 40
Spencer Uresk Avatar answered Sep 29 '22 09:09

Spencer Uresk