大家好,我已经使用 hybernate + JPA 完成了应用程序配置,以及用于 XA 事务管理和 spring 3.0 的 atomikos 和 mysql 是我的后端,这里一切正常,但是插入操作,当抛出异常时事务应该回滚,但它是没有发生!!这是我们应用程序的一个小流程,在我们的经理级别,我们正在调用业务(这里我们使用 Spring IOC)我的
Manager.java
insertuser()
{
//here we are getting transcation support from spring.
business.insertuser();
}
business.java 我们使用一种方法 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.
}
而我们的 dao.java 类,我们正在使用一种方法 insertuser(Object entity)
void insertDetails(Object entity)
{
this.getJpaTemplate().persist(entity);
}
和我们的 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>
我的 persistence.xml 文件是
<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.TransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
我在 application/meta_inf/context.xml 中配置了我的 jndi
<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>
我的配置文件是 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>
问题在哪里?实际上,当我尝试更新表中的某些属性时,事务工作正常(正在发生回滚和提交),但是当我尝试执行插入操作时,回滚没有发生。