JPA transaction 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 and mysql is my backend,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>
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开发者_StackOverflow社区.manager_lookup_class"
value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
</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.MysqlXADataSource"
factory="com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory"
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</beans:prop>
</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.LocalContainerEntityManagerFactoryBean">
<beans:property name="persistenceUnitName" value="shop" />
<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:bean>
</beans:property>
<beans:property name="persistenceXmlLocation">
<bean: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.
The more I look at this, the more confused it looks.
First, how does the AOP config relate to your actual methods? The way it is configured, it will only create transactions around methods starting with "save" or "remove", and read-only transactions around everything else. Your method is called insertDetails
, so that won't work.
Second, do you have a Spring PlatformTransactionManager
configured? I don't see the configuration.
Third: Looks like you're using the default package for your classes, but defined the pointcut for classes in a package com.live.webAppl
. Is that where your classes really are?
Fourth: Is there an open transaction at the point where the exception is thrown, and did the inserting method participate in that transaction? If not, then it cannot rollback. Use the debugger together with the log output to see where which transaction is created and if the insert method participates. Try raising Atomikos' log level to debug, too.
Fourth, are you stuck in a Java 1.4 codebase? Otherwise, go for annotational transaction declaration. (A lot of the ways things are done in your config and code look a little dated.)
Fifth, the convention is to use all lowercase package names. Classes start with a capital letter, members and locals lowercase. This will help the readability of your code. (For example, Dao.insertUser()
looks like a static method call to anyone who can read Java. Not good.)
精彩评论