Hi all,
I've having trouble bigtime with setting upp Spring transactions. I've put together a minimal example but can't get it to work.
TestServiceImpl:
CarDao:Code:public class TestServiceImpl implements TestService{ private CarDao carDao; @Transactional(propagation = Propagation.REQUIRES_NEW) public void addCar(Car car) { carDao.addCar(car); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void addCars(List<Car> cars) { carDao.addCars(cars); } public List<Car> getCars() { return carDao.getCars(); } public Car getCar(int id) { return carDao.getCar(id); } public void setCarDao(CarDao carDao) { this.carDao = carDao; } }
Code:public class CarDao { private EntityManager entityManager; @PersistenceContext public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } public void addCar(Car car){ System.out.println("Persisting car"); entityManager.persist(car); //getJpaTemplate().persist(car); } public void addCars(List<Car> cars){ System.out.println("Persisting cars"); for(Car c : cars) entityManager.persist(c); System.out.println("Done Persisting cars"); } public List<Car> getCars(){ List<Car> cars = new ArrayList<Car>(); return cars; } public Car getCar(int id){ //return getJpaTemplate().find(Car.class, id); return entityManager.find(Car.class, id); } }applicationContextCode:<tx:advice id="txAdvice" transaction-manager="transactionManager">
When I deploy in tomcat i Get this:Code:... <bean id="lrfDB" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/lrfmedia?createDatabaseIfNotExist=true"/> <property name="username" value="lrfmedia"/> <property name="password" value="lrfmedia"/> </bean> <bean id="testFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="testFactory"/> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> </props> </property> <property name="dataSource" ref="lrfDB"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL"/> <property name="showSql" value="true"/> </bean> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="testFactory"/> <property name="dataSource" ref="lrfDB"/> <property name="jpaDialect" > <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/> </property> </bean> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean id="testService" class="se.bjurek.test.service.TestServiceImpl"> <property name="carDao" ref="issueDao"/> </bean> <bean id="issueDao" class="dao.CarDao"> </bean> ...
But when I call the addCars the data never gets persisted and the logs never says "Creating new transaction with name..."Code:... 2009-05-19 00:10:27,890 [main] DEBUG [org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method[*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT] 2009-05-19 00:10:27,890 [main] DEBUG [org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method[*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT] ...
I've also tried with JpaDaoSupport and you can see the commented code in the dao but that didn't work either.
Any help is appreciated.


Reply With Quote
