最近在配置HIBERNATE+SPRING事务的时候总是出现问题,想向大家请教一下
具体表现为两个REQUIRED函数连续调用,第一个函数调用成功,第二个函数调用抛出异常,但是第一个函数没有ROLLBACK而是COMMIT了,不符合事物传播的规则
查看日志也是这样,第一个函数在调用完成后就COMMIT了,而没有保持和第二个函数在同一个transaction中
具体配置如下:
请大家无论如何都要帮我下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="sys"/>
<property name="password" value=""/>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Person.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* business.*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<bean id="person" class="business.PersonImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>package business;import org.hibernate.SessionFactory;public interface Person {
public void save(entity.Person person);
public void delete(entity.Person person);}import org.springframework.orm.hibernate3.HibernateTemplate;import entity.Person;
public class PersonBusiness {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");
Person person = new Person();
p.save(person);
p.delete(person);
}}package business;import org.hibernate.SessionFactory;public class PersonImpl implements Person {
private SessionFactory sf;
public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}
public void save(entity.Person person) {
sf.getCurrentSession().save(person);
}
public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}}
具体表现为两个REQUIRED函数连续调用,第一个函数调用成功,第二个函数调用抛出异常,但是第一个函数没有ROLLBACK而是COMMIT了,不符合事物传播的规则
查看日志也是这样,第一个函数在调用完成后就COMMIT了,而没有保持和第二个函数在同一个transaction中
具体配置如下:
请大家无论如何都要帮我下
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="sys"/>
<property name="password" value=""/>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Person.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* business.*.*(..))" advice-ref="txAdvice"/>
</aop:config>
<bean id="person" class="business.PersonImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>package business;import org.hibernate.SessionFactory;public interface Person {
public void save(entity.Person person);
public void delete(entity.Person person);}import org.springframework.orm.hibernate3.HibernateTemplate;import entity.Person;
public class PersonBusiness {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");
Person person = new Person();
p.save(person);
p.delete(person);
}}package business;import org.hibernate.SessionFactory;public class PersonImpl implements Person {
private SessionFactory sf;
public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}
public void save(entity.Person person) {
sf.getCurrentSession().save(person);
}
public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}}
你把他们俩方到一个方法里
testTran(entity.Person person){
p.save(person);
p.delete(person);
}
<tx:attributes>
<tx:method name="testTran" propagation="REQUIRED" />
</tx:attributes>
这样就可以了。
不建议在DAO层做事务处理,可以在逻辑层处理事务
sf.getCurrentSession().save(person);
delete();//假如你在这里调用delete方法,那在执行时他们是同一个事务
}那 ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");
Person person = new Person();
p.save(person);假如你的代码是这样的,那你执行save方法时,在执行里面的delete()时,抛出异常,delete会回滚,save也会回滚,因为这时他们是同一个事务,
<?xml version="1.0" encoding="UTF-8"?><!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 通过hibernate.cfg.xml配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<!-- 引用 配置SessionFactory的ID-->
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 哪些方法有 事务的传播性-->
<tx:attributes>
<!-- 以add开始的方法 REQUIRED表示就用事务,没有就开启事务-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="select*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!-- 除了上面的方法外,其余的方法都是只读 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 哪些类的方法参与了事务 -->
<aop:config>
<!-- execution(* com.east.spring.managerimpl.*.*(..))这个类的所有方法都用事务 -->
<aop:pointcut id="allManagerMethod" expression="execution(* com.east.spring.manager.impl.*.*(..))"/>
<!-- 引用 pointcut 和 advice-->
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
我记得楼主这样配置,应该没什么问题,调用的时候是各用各的事务,事务只是session级别的;顺便问句:
这样的话,可能不抛异常吗?
是代码问题吧!
public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}