2 Replies Latest reply on Jul 10, 2009 5:50 AM by Muhammed Patel

    Spring + Transaction Manager problems

    Muhammed Patel Newbie

      Hi there again,

      I am having a huge proble right now and i am wondering if anyone can please help me.

      i am using Bitronix Transaction manager, Spring 2.5.6 and Tomcat and trying to get Envers to work with this but still no luck.

      Right now i am getting the follwoing error when calling the transaction manager

      java.lang.NullPointerException
       org.hibernate.transaction.JTATransaction.registerSynchronization(JTATransaction.java:313)
       org.hibernate.envers.synchronization.AuditSyncManager.get(AuditSyncManager.java:56)
       org.hibernate.envers.event.AuditEventListener.onPostUpdate(AuditEventListener.java:161)
       org.hibernate.action.EntityUpdateAction.postUpdate(EntityUpdateAction.java:200)
       org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:179)
       org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
       org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
       org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
       org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
       org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
       org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
       org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
       org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
       org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
       org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)
       za.co.aforbes.fpc.db.dao.BaseHibernateDao.performSave(BaseHibernateDao.java:92)
       za.co.aforbes.fpc.db.dao.BaseHibernateDao.save(BaseHibernateDao.java:79)
       za.co.aforbes.fpc.db.dao.BaseHibernateDao.save(BaseHibernateDao.java:1)
       za.co.aforbes.fpc.bl.client.impl.AddressServiceImpl.save(AddressServiceImpl.java:89)
       za.co.aforbes.fpc.bl.client.impl.AddressServiceImpl.save(AddressServiceImpl.java:1)
       za.co.aforbes.fpc.ui.bean.table.EditDataTableBean.save(EditDataTableBean.java:113)
       za.co.aforbes.fpc.ui.bean.client.AddressBean.save(AddressBean.java:169)
       za.co.aforbes.fpc.ui.bean.client.AddressBean.save(AddressBean.java:1)
       za.co.aforbes.fpc.ui.bean.table.EditDataTableBean.performSave(EditDataTableBean.java:89)
       za.co.aforbes.fpc.ui.bean.BaseFormBean.save(BaseFormBean.java:49)
       sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       java.lang.reflect.Method.invoke(Method.java:597)
      


      My application context is set up as the following. Please let me know if you can see what i am doing wrong.

      <?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       ">
       <!-- START - IFA DATASOURCE AND SESSION FACTORY-->
       <!--
       Below are connection values to the database. These values are used in
       the hibernate.properties file and are populated at runtime
       -->
      
      
      
       <bean id="transactionManager"
       class="org.springframework.transaction.jta.JtaTransactionManager"
       depends-on="bitronixTransactionManager"> <property
       name="transactionManager" ref="bitronixTransactionManager" />
       <property name="userTransaction" ref="bitronixTransactionManager" />
       </bean>
      
       <!-- Bitronix Transaction Manager embedded configuration
       -->
       <bean id="btmConfig" class="bitronix.tm.TransactionManagerServices"
       factory-method="getConfiguration">
      
       <property name="serverId" value="spring-btm" />
       </bean>
      
       <!-- create Bitronix Transaction Manager transaction manager -->
       <bean id="bitronixTransactionManager" class="bitronix.tm.TransactionManagerServices"
       factory-method="getTransactionManager" depends-on="btmConfig"
       destroy-method="shutdown" />
      
      
      
      
      
      
      
       <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName">
       <value>java:comp/env/jdbc/ifaDB</value>
       </property>
       </bean>
      
      
       <bean id="msqlDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource"
       init-method="init" destroy-method="close">
       <property name="className"
       value="com.microsoft.sqlserver.jdbc.SQLServerXADataSource" />
       <property name="allowLocalTransactions" value="true" />
      
       <property name="uniqueName" value="jdbc/ifa" />
       <property name="minPoolSize" value="0" />
       <property name="maxPoolSize" value="5" />
       <property name="driverProperties">
       <props>
       <prop key="databaseName">ifa</prop>
       <prop key="user">ddd</prop>
       <prop key="password">ddd</prop>
      
      
       </props>
       </property>
       </bean>
      
      
      
      
       <bean id="ifaSessionFactory"
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="jtaTransactionManager">
       <ref bean="bitronixTransactionManager"></ref>
       </property>
       <property name="useTransactionAwareDataSource" value="true" />
       <property name="dataSource">
       <ref bean="dataSource" />
       </property>
      
       <property name="configLocation">
       <value>WEB-INF/hibernate/hibernate-ifa.cfg.xml</value>
       </property>
       <property name="configurationClass">
       <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
       <props>
      
       <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
       <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
       <prop key="hibernate.show_sql">true</prop>
       <prop key="hibernate.connection.datasource">jdbc/ifa</prop>
       <prop key="hibernate.transaction.auto_close_session">true</prop>
       <prop key="jta.UserTransaction">java:comp/UserTransaction</prop>
       <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
       </prop>
       <prop key="hibernate.current_session_context_class">thread</prop>
       <!--
       <prop key="hibernate.hbm2ddl.auto">update</prop>
       -->
       <prop key="hibernate.jndi.class">bitronix.tm.jndi.BitronixInitialContextFactory</prop>
       <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.BTMTransactionManagerLookup
       </prop>
       <prop key="hibernate.format_sql">true</prop>
       <prop key="hibernate.connection.release_mode">after_statement</prop>
       <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider </prop>
       </props>
      
      
      
      
       </property>
       <property name="eventListeners">
       <map>
       <entry key="post-insert">
       <list>
      
       <ref bean="ejbInsert" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="post-update">
       <list>
      
       <ref bean="ejbUpdate" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="post-delete">
       <list>
      
      
       <bean class="org.hibernate.ejb.event.EJB3PostDeleteEventListener" />
       <ref bean="audit" />
       </list>
       </entry>
       <entry key="pre-collection-update">
       <ref bean="audit" />
       </entry>
       <entry key="pre-collection-remove">
       <ref bean="audit" />
       </entry>
       <entry key="post-collection-recreate">
       <ref bean="audit" />
       </entry>
       </map>
       </property>
      
       </bean>
       <bean name="callBack" class="org.hibernate.ejb.event.EntityCallbackHandler" />
      
       <bean name="audit" class="org.hibernate.envers.event.AuditEventListener" />
       <bean name="ejbInsert" class="org.hibernate.ejb.event.EJB3PostInsertEventListener">
       <property name="callbackHandler">
       <ref bean="callBack" />
       </property>
       </bean>
       <bean name="ejbUpdate" class="org.hibernate.ejb.event.EJB3PostUpdateEventListener">
       <property name="callbackHandler">
       <ref bean="callBack" />
       </property>
       </bean>
      
      
      <bean id="hibernateDao"
       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
       <property name="transactionManager" ref="transactionManager" />
       <property name="transactionAttributes">
       <props>
       <prop key="*">PROPAGATION_REQUIRED, -Exception
       </prop>
       </props>
       </property>
      
      
       </bean>
      
      <bean id="addresser" parent="hibernateDao">
      <property name="target">
      <ref bean="addressDao"/>
      </property>
      </bean>
      <bean id="countrar" parent="hibernateDao">
      <property name="target">
      <ref bean="countryDao"/>
      </property>
      </bean>
      <bean id="addressertype" parent="hibernateDao">
      <property name="target">
      <ref bean="addressTypeDao"/>
      </property>
      </bean>
      
      
       <!-- END - IFA DATASOURCE AND SESSION FACTORY-->
       <!-- START - COMMONWORKS DATASOURCE AND SESSION FACTORY-->
       <!--
       Below are connection values to the database. These values are used in
       the hibernate.properties file and are populated at runtime
       -->
       <bean id="commonWorksDataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName">
       <value>net.sourceforge.jtds.jdbc.Driver</value>
       </property>
       <property name="url">
       <value>jdbc:jtds:sqlserver://localhost:1433/CommonWorks;prepareSQL=0;SendStringParametersAsUnicode=False;
       </value>
       </property>
       <property name="username">
       <value>ddd</value>
       </property>
       <property name="password">
       <value>dddd</value>
       </property>
       <property name="initialSize">
       <value>2</value>
       </property>
       <property name="maxActive">
       <value>5</value>
       </property>
       <property name="maxIdle">
       <value>2</value>
       </property>
       </bean>
       <bean id="commonWorksSessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource">
       <ref bean="commonWorksDataSource" />
       </property>
       <property name="configLocation">
       <value>WEB-INF/hibernate/hibernate-commonworks.cfg.xml</value>
       </property>
       <property name="configurationClass">
       <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
       <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
       <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
       <prop key="hibernate.show_sql">true</prop>
       <prop key="hibernate.format_sql">true</prop>
       </props>
       </property>
       </bean>
       <!-- END - COMMONWORKS DATASOURCE AND SESSION FACTORY-->
       <!--
       Configurer that replaces ${...} placeholders with values from
       properties files
       -->
       <!-- (in this case, mail and JDBC related properties) -->
       <bean id="propertyConfigurer"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
       <list>
       <value>WEB-INF/spring/mail.properties</value>
       <value>WEB-INF/spring/jdbc.properties</value>
       </list>
       </property>
       </bean>
       <!-- MailSender used by EmailAdvice -->
       <!--
       <bean id="mailSender"
       class="org.springframework.mail.javamail.JavaMailSenderImpl">
       <property name="host" value="${mail.host}"/> </bean>
       -->
      </beans>
      



      Any help would be much appreciated . I am sure this is a silly config error somewhere but i can't find it.

      :)

      Many Thanks,
      Muhammed Patel

        • 1. Re: Spring + Transaction Manager problems
          Muhammed Patel Newbie

          I seem to have found my problem

          i had to wrap my dao in the transaction for it to work





          PROPAGATION_REQUIRED, -Exception















          Hope this helps someone.

          • 2. Spring + Transaction Manager problems
            Muhammed Patel Newbie

            Hi there,

            It seems that i was being a little silly and all i had to do is wrap the dao in the transaction for it to work.

            <bean id="hibernateDao"
             class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
             <property name="transactionManager" ref="transactionManager" />
             <property name="transactionAttributes">
             <props>
             <prop key="*">PROPAGATION_REQUIRED, -Exception
             </prop>
             </props>
             </property>
            
            
             </bean>
            
            <bean id="addresser" parent="hibernateDao">
            <property name="target">
            <ref bean="addressDao"/>
            </property>
            </bean>


            I hope this helps someone.

            Thanks,
            Muhammed Patel