Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: JPA transcation rollBack is not working

  1. #1

    Default JPA transcation rollBack is not working

    Hi all
    I have done an application configuration using hybernate + JPA ,and atomikos for XA transcation management and spring 3.0 ,here every thing is working fine however insert operation, when exception is throwing the transcation should rollback,but it is not happening!!
    here is a small flow for our application, in our manager level we are calling the businesss (here we are using Spring IOC)
    my Manager.java
    insertuser()
    {
    //here we are getting transcation support from spring.

    business.insertuser();
    }
    business.java we are using one method insertuser()
    insertuser()
    {
    Tauser taUser=new Tauser();
    taUser.setUsername("Maya");
    taUser.setPassword("*****")
    Dao.insertDetails(taUser);
    throw new NullPointerException("checking transcation management"); // because of this exception throwing,it should rollback right,but its not happening.The property's are commiting in to the table.

    }
    and our dao.java class we are using one method insertuser(Object entity)

    void insertDetails(Object entity)
    {
    this.getJpaTemplate().persist(entity);
    }
    and our
    orm.xml

    <entity class="TaUser" name="TaUser">
    <table name="ta_user" />
    <attributes>
    <id name="userId">
    <column name="USER_ID" />
    <generated-value strategy="AUTO" />
    </id>
    <basic name="userName">
    <column name="USER_NAME" length="50" />
    </basic>
    </attributes>
    </entity>
    and my persistence.xml file is

    <persistence-unit name="shop" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence </provider>
    <jta-data-source>java:comp/env/jdbc/shobWeb</jta-data-source>
    <mapping-file>META-INF/orm.xml</mapping-file>
    <class> TaUser</class>
    ---------
    ---------
    ---------
    <properties>
    <property name="hibernate.transaction.manager_lookup_class"
    value="com.atomikos.icatch.jta.hibernate3.Transact ionManagerLookup"/>
    </properties>
    </persistence-unit>
    </persistence>
    and i configured my jndi in
    application/meta_inf/context.xml
    <Resource name="jdbc/shobWeb" auth="Container"
    driverClassName="com.mysql.jdbc.Driver"
    user="root"
    password="root"
    type="com.mysql.jdbc.jdbc2.optional.MysqlXADataSou rce"
    factory="com.mysql.jdbc.jdbc2.optional.MysqlDataSo urceFactory"
    url="jdbc:mysql://localhost:3306/shobWebSample"
    explicitUrl="true"
    pinGlobalTxToPhysicalConnection="true"
    ></Resource>
    and my config file is
    config.xml

    <beans:bean id="Manager"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <beans: property name="transactionManager">
    <beans: ref bean="transactionManager" />
    </beans: property>
    <beans: property name="target">
    <beans: ref local=" ManagerTarget" />
    </beans: property>
    <beans: property name="transactionAttributes">
    <beans: props>
    <beans: prop key="*">PROPAGATION_REQUIRED</beansrop>
    </beans: props>
    </beans: property>
    </beans: bean>

    <beans: bean id="ManagerTarget"
    class="Manager">
    <beans: property name="Business" ref="Business" />
    </beans: bean>
    <beans: bean id="Business" class="PaymentsBusiness">
    <beans: property name="Dao" ref=" Dao" />
    </beans:bean>

    <beans:bean id="Dao"
    class=" Dao">
    <beans: property name="jpaTemplate">
    <beans: ref bean="jpaTemplate" />
    </beans: property>
    </beans:bean>

    <beans:bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    <beans: property name="entityManagerFactory">
    <beans: ref bean="entityManagerFactory" />
    </beans: property>
    </beans:bean>


    <beans: bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <beans: property name="persistenceUnitName" value="shop" />
    <beans: property name="jpaVendorAdapter">
    <beans:bean
    class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <beans: property name="generateDdl" value="false" />
    <beans: property name="showSql" value="true" />
    </beans: bean>
    </beans: property>
    <beans: property name="persistenceXmlLocation">
    <beans :value>classpath:META-INF/persistence.xml</beans: value>
    </beans: property>
    </beans: bean>
    where is the issue? Actually when i am trying to update some property in table then transcation is working fine (rollback and commit is happening ),but when i am trying to do insert operation rollback is not happening.
    Last edited by maya; Dec 8th, 2010 at 07:49 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    1) Don't use (Transaction)ProxyFactoryBean it is out-dated more or less, use aop:config and tx:advice
    2) Don't use JpaTemplate thta is also out-dated/not a best practice anymore
    3) You use MySQL make sure you have transactional tables (InnoDB) and not MyiSAM tables.
    4) Make sure you use the proxied instance of your manager (this is related to 1 because you use a ProxyFactoryBean).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks for your reply . I am going to try that, what you mentioned above ... but if i use <generated-value strategy="TABLE" /> means transaction rollback is working fine ..but it should generating six digit id's. could u tel why ?
    and
    from your third suggestion ------actually i am new for jpa .. instead of JpaTemplate what i have to use ?

    thanks
    maya
    Last edited by maya; Dec 9th, 2010 at 07:34 AM.

  4. #4

    Default

    HI Marten Deinum
    Based on your suggestion i changed my coding ,but still the same issue is coming.My updated codes here

    my Manager.java
    Code:
    insertuser()
    	     {
    		  private static ApplicationContext springCtx;
    		 if (springCtx == null) {
    			springCtx = (ApplicationContext) FacesContextUtils
    					.getRequiredWebApplicationContext(FacesContext
    							.getCurrentInstance());
    		}
    		 return springCtx.getBean("Manager");
    		  here  our transcation  will start 
    	      business.insertuser();
    	     }
    business.java we are using one method insertuser()
    Code:
       insertuser()
              {
                Tauser taUser=new Tauser();
                taUser.setUsername("Maya");
                taUser.setPassword("*****")       
    			Dao.insertDetails(taUser);
    			 throw new NullPointerException("checking transcation management"); // because  of this exception throwing,it should rollback right,but its not happening.The property's are commiting in to the table.
                  
             }
    and our dao.java class we are using one method insertuser(Object entity)
    Code:
    	import javax.persistence.EntityManager;
    import org.hibernate.Criteria;
    import org.hibernate.Query;
    import org.hibernate.Session; 
    import org.hibernate.ejb.HibernateEntityManager;
    import org.springframework.orm.jpa.support.JpaDaoSupport;
    import org.springframework.util.Assert;
          public class dao extends JpaDaoSupport
    	  {
        void insertDetails(Object entity)
           {
    	          
            this.getJpaTemplate().persist(entity);     
    //after this line  its committing data into DB it should not happen  here ????? 
           }
    	   }
    and our orm.xml we are doing
    Code:
    		   <entity class="TaUser" name="TaUser">
            <table name="ta_user" />
            <attributes>
                <id name="userId">
                    <column name="USER_ID" />
                    <generated-value strategy="AUTO" />
                </id>
                <basic name="userName">
                    <column name="USER_NAME" length="50" />
                </basic>   
                </attributes>
        </entity>
    and my pojo class is

    TaUser.java (here we are not using annotations so we mapping with orm.xml file )
    Code:
    public class TaUser implements java.io.Serializable 
    	{
    	private int userId;
    	private String userName;
    	
    	public int getUserId() {
    		return this.userId;
    	}
    
    	public void setUserId(int userId) {
    		this.userId = userId;
    	}
    	public String getUserName() {
    		return this.userName;
    	}
    
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    		
    	}
    and my persistence.xml file is

    Code:
     <persistence-unit name="webhub" transaction-type="JTA">
    	   <provider>org.hibernate.ejb.HibernatePersistence</provider>
    	     <jta-data-source>java:comp/env/jdbc/shobWeb</jta-data-source>
    	       <mapping-file>META-INF/orm.xml</mapping-file>
               <class>TaUser</class>
    		   ---------
    		   ---------
    		   ---------
    		   <properties>
                <property name="hibernate.transaction.manager_lookup_class" 
                value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
    			</properties>     
    	</persistence-unit>
      </persistence>
    and i configured my jndi in
    Code:
      application/meta_inf/context.xml
      <Resource name="jdbc/shobWeb" auth="Container"  
                          driverClassName="com.mysql.jdbc.Driver"  
                          user="root"
                          password="root"  
                          type="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"   
                          factory="com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory" 
                          url="jdbc:mysql://localhost:ebSample"
                          explicitUrl="true" 
                          pinGlobalTxToPhysicalConnection="true" 
                          ></Resource>
    and my config file is ,

    my config.xml is
    Code:
    					 <beans:bean id="Manager"
    		class="Manager">
    		<beans:property name="Business" ref="Business" />
    	</beans:bean>
    	<beans:bean id="Business" class="Business">
    		<beans:property name="Dao" ref="Dao" />		 
    	</beans:bean>
    				
    
    <beans:bean id="Dao"
    		class="Dao">
    		<beans:property name="jpaTemplate">
    			<beans:ref bean="jpaTemplate" />
    		</beans:property>
    	</beans:bean>
    
     	<beans:bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    		<beans:property name="entityManagerFactory">
    			<beans:ref bean="entityManagerFactory"/>
    		</beans:property>
    	</beans:bean> 
    
    	<beans:bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<beans:property name="persistenceUnitName" value="webhub" />
    		<beans:property name="jpaVendorAdapter">
    			<beans:bean
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<beans:property name="generateDdl" value="false" />
    				<beans:property name="showSql" value="true" />
    				<beans:property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
    			</beans:bean>
    		</beans:property>
    		<beans:property name="persistenceXmlLocation">
    			<beans:value>classpath:META-INF/persistence.xml</beans:value>
    		</beans:property>
    		<beans:property name="persistenceUnitManager">
    			<beans:ref bean="persistenceUnitManager"/>
    		</beans:property>
    	</beans:bean>
    
     <beans:bean id="persistenceUnitManager"  class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
     </beans:bean>		
     here  i Configuread AOP Like this 
        <aop:config>
             	<aop:pointcut id="fooServiceOperation"  expression="execution(* com.live.webAppl.*.*.*(..))"/>          
              <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
              </aop:config>
        <tx:advice id="txAdvice">
         <tx:attributes>
                <tx:method name="save*" rollback-for="UserExistsException"/>
                <tx:method name="remove*"/>
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    my log trace information about transaction is

    DEBUG - AbstractPlatformTransactionManager.handleExistingT ransaction(470) | Participating in existing transaction
    Hibernate: insert into ta_user (created_USER_IDdate, userName) values (?, ?)
    DEBUG - AbstractPlatformTransactionManager.processRollback (850) | Participating transaction failed - marking existing transaction as rollback-only
    DEBUG - JtaTransactionManager.doSetRollbackOnly(1060) | Setting JTA transaction rollback-only
    134578 [http-8080-Processor23] INFO atomikos - setRollbackOnly() called for transaction PaymentsTransactions0000100653
    DEBUG - AbstractPlatformTransactionManager.processRollback (843) | Initiating transaction rollback
    175094 [http-8080-Processor23] INFO atomikos - afterCompletion ( STATUS_ROLLEDBACK ) called on Synchronization: org.hibernate.ejb.AbstractEntityManagerImpl$1@c5c2 3d
    175094 [http-8080-Processor23] INFO atomikos - afterCompletion ( STATUS_ROLLEDBACK ) called on Synchronization: org.hibernate.transaction.CacheSynchronization
    175094 [http-8080-Processor23] INFO atomikos - rollback() done of transaction PaymentsTransactions0000100653
    But still I am getting the same rollback problem.Could you give me any solution
    Last edited by maya; Dec 10th, 2010 at 04:07 AM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    1) rewrite your pointcut 'execution(* com.live.webAppl.*.*.*(..))' to 'execution(* com.live.webAppl..*.*(..))' also your pointcut matches only in the com.live.webAppl package (why is there an uppercase?! package names should be lower case).

    2) Why are you getting a new dao?! You are already injecting your Manager class with the Business etc.... Your Manager should be the one from the applicationcontext, if it isn't your configuration is useless.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6

    Default

    1 by mistaken i wrote in froum .. Thats not my real package name .. 'execution(* com.live.webAppl.*.*.*(..))' to 'execution(* com.live.webAppl..*.*(..))'.. this is working fine ...
    2 i have to check .. can you sure because of this only rollback is not working properly ?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If it isn't spring managed the transaction isn't around your manager object but around your dao, committing the data after the dao method. Which leads to this result...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8

    Default

    actually i am new for spring and JPA .. i understood what you are telling .. could you post any sample coding for achieving this ?
    Last edited by maya; Dec 10th, 2010 at 07:51 AM.

  9. #9

    Default

    Marten Deinum
    still i am waiting for your reply ...

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I already gave you the solution, all the other information is in the reference guide...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •