I know this has been asked before, thats why I've spent the past 2 days trying to get @Transactional to work with no luck before asking for help.
In my applicationContext.xml:
- I create a JpaTransactionManager
- In my <tx:annotation-driven... I explicitly set the reference to my transaction manager
- I define my service class (with transactional support) as a bean.
When I (from my MVC layer) call the services annotated method no transaction it created. I've tried adding the <tx:annotation-driven... in my dispatcherServlet.xml as well with no luck.
BTW: I know the aspectj stuff works because its working for the @Cachable annotated methods.
ClientService.java
public class ClientService{
...
@Transactional
public Client editClient(Integer clientId, Client newClientState){
logger.debug("Transaction active:::: {}", TransactionSynchronizationManager.isActualTransactionActive());
Client dbClient = this.repository.getClient(clientId);
//Copy new state into dbClient
this.repository.save(dbClient);
return dbClient;
}
}
pom.xml
<properties>
<hibernate.version>3.6.0.Final</hibernate.version>
<hibernate.validator.version>4.1.0.Final</hibernate.validator.version>
<spring.version>3.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency><groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version></dependency>
<dependency><groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version></dependency>
<dependency><groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.validator.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version></dependency>
<dependency><groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version></dependency>
...
</dependencies>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
....
<cache:annotation-driven cache-manager="cacheManager" mode="aspectj"/>
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
....
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="mainDataSource" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<!-- Service -->
<bean class="...ClientService" />
....
</beans>