Can't get Spring transactions to work
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: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(Listlt;Cargt; cars) { carDao.addCars(cars); }
public Listlt;Cargt; getCars() { return carDao.getCars(); }
public Car getCar(int id) { return carDao.getCar(id); }
public void setCarDao(CarDao carDao) { this.carDao = 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(quot ersisting carquot;); entityManager.persist(car); //getJpaTemplate().persist(car); } public void addCars(Listlt;Cargt; cars){ System.out.println(quot ersisting carsquot;); for(Car c : cars) entityManager.persist(c); System.out.println(quot;Done Persisting carsquot;); } public Listlt;Cargt; getCars(){ Listlt;Cargt; cars = new ArrayListlt;Cargt;(); return cars; } public Car getCar(int id){ //return getJpaTemplate().find(Car.class, id); return entityManager.find(Car.class, id); }}
Code:
lt;tx:advice id=quot;txAdvicequot; transaction-manager=quot;transactionManagerquot;gt;
applicationContextCode:
...
lt;bean id=quot;lrfDBquot; class=quot;org..jdbc.datasource.DriverManagerDataSourcequot;gt; lt;property name=quot;driverClassNamequot; value=quot;com.mysql.jdbc.Driverquot;/gt; lt;property name=quot;uclquot; value=quot;jdbc:mysql--localhost:3306/lrfmedia?createDatabaseIfNotExist=truequot;/gt; lt;property name=quot;usernamequot; value=quot;lrfmediaquot;/gt; lt;property name=quot;passwordquot; value=quot;lrfmediaquot;/gt; lt;/beangt;
lt;bean id=quot;testFactoryquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt; lt;property name=quot;persistenceUnitNamequot; value=quot;testFactoryquot;/gt; lt;property name=quot;jpaPropertiesquot;gt;lt;propsgt; lt;prop key=quot;hibernate.dialectquot;gt;org.hibernate.dialect.MySQLDialectlt;/propgt; lt;prop key=quot;hibernate.hbm2ddl.autoquot;gt;create-droplt;/propgt; lt;prop key=quot;hibernate.connection.driver_classquot;gt;com.mysql.jdbc.Driverlt;/propgt; lt;/propsgt; lt;/propertygt; lt;property name=quot;dataSourcequot; ref=quot;lrfDBquot;/gt; lt;property name=quot;jpaVendorAdapterquot;gt;lt;bean class=quot;org..orm.jpa.vendor.HibernateJpaVendorAdapterquot;gt; lt;property name=quot;databasequot; value=quot;MYSQLquot;/gt; lt;property name=quot;showSqlquot; value=quot;truequot;/gt;lt;/beangt; lt;/propertygt; lt;/beangt;
lt;bean id=quot;transactionManagerquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt; lt;property name=quot;entityManagerFactoryquot; ref=quot;testFactoryquot;/gt; lt;property name=quot;dataSourcequot; ref=quot;lrfDBquot;/gt; lt;property name=quot;jpaDialectquot; gt;lt;bean class=quot;org..orm.jpa.vendor.HibernateJpaDialectquot;/gt; lt;/propertygt; lt;/beangt;
lt;bean class=quot;org..orm.jpa.support.PersistenceAnnotationBeanPostProcessorquot;/gt;
lt;bean id=quot;testServicequot; class=quot;se.bjurek.test.service.TestServiceImplquot;gt; lt;property name=quot;carDaoquot; ref=quot;issueDaoquot;/gt; lt;/beangt;
lt;bean id=quot;issueDaoquot; class=quot;dao.CarDaoquot;gt; lt;/beangt;
...
When I deploy in tomcat i Get this:
Code:
...
2009-05-19 00:10:27,890 [main] DEBUG [org..transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method[*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2009-05-19 00:10:27,890 [main] DEBUG [org..transaction.interceptor.NameMatchTransactionAttributeSource] - Adding transactional method[*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
...
But when I call the addCars the data never gets persisted and the logs never says quot;Creating new transaction with name...quot;
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.
I suggest you read chapter 9 of the reference guide.
You are mixing transaction strategies you are trying to use annotations with a plain xml configuration. Use 1 or the other. Currently you use both and both are not sufficiently configured.
To make the annotations work you will need a 'lt;tx:annotation-driven /gt;' in your configuration. To make the 'lt;tx:advice /gt;' work you will need to include some pointcuts enclosed in a 'aop:config' block.
Currently you have both misconfigured.
Originally Posted by Marten DeinumI suggest you read chapter 9 of the reference guide.
You are mixing transaction strategies you are trying to use annotations with a plain xml configuration. Use 1 or the other. Currently you use both and both are not sufficiently configured.
To make the annotations work you will need a 'lt;tx:annotation-driven /gt;' in your configuration. To make the 'lt;tx:advice /gt;' work you will need to include some pointcuts enclosed in a 'aop:config' block.
Currently you have both misconfigured.Hi,
I've paste the wrong setup. I use the annotation-driven-syntax in the applicationContext-file.
I found what caused the problem today. I had put the lt;tx:annotation-driven/gt; inside a config-file and the transactionmanager-bean in another and pointed out the location of the file in the web.xml. That didn't work, when I include one config-file in the other everything works. |