Announcement Announcement Module
Collapse
No announcement yet.
Can't get Spring transactions to work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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(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;
        }
    }
    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);
        }
    
    
    }
    Code:
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    applicationContext

    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>
    ...
    When I deploy in tomcat i Get this:
    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]
    ...
    But when I call the addCars the data never gets persisted and the logs never says "Creating new transaction with name..."

    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.

  • #2
    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 '<tx:annotation-driven />' in your configuration. To make the '<tx:advice />' work you will need to include some pointcuts enclosed in a 'aop:config' block.

    Currently you have both misconfigured.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      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 '<tx:annotation-driven />' in your configuration. To make the '<tx:advice />' 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 <tx:annotation-driven/> 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.

      Comment

      Working...
      X