YY爱CC 2014年01月12日
spring中hibernate的事务回滚
项目架子:servlet+spring+hibernate+oracle
那个Action大致是这样的,这里面涉及到两个数据的插入,一个是merchantDAO.save(merchant);一个是userDAO.save(user);我想用spring事务控制,如果user插入失败,则之前插入的merchant回滚,我试了试,没实现,估计是我配置文件没写对。
package action.merchant;
public class AddMerchantAction extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ApplicationContext ctx = SpringInitListener.getCtx();

//添加商户
MerchantInfoDAO merchantDAO = MerchantInfoDAO.getFromApplicationContext(ctx);
MerchantInfo merchant = new MerchantInfo();

merchantDAO.save(merchant);

//添加User
UserInfoDAO userDAO = UserInfoDAO.getFromApplicationContext(ctx);
UserInfo user = new UserInfo();
userDAO.save(user);

}
}


配置文件代码大致如下:
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@192.166.68.42:1521:ctccpos"></property>
<property name="username" value="ctcc"></property>
<property name="password" value="oracle"></property>
<property name="maxActive" value="1000" />
<property name="maxIdle" value="50" />
<property name="minIdle" value="20" />
<property name="maxWait" value="1000" />
<property name="defaultAutoCommit" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1200000" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1000"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">util.CustomerDialect</prop>
<prop key="hibernate.connection.charSet">GBK </prop>
<prop key="hibernate.connection.characterEncoding">GBK </prop>
<prop key="myeclipse.connection.profile">OracleThin Conn </prop>
</props>
</property>
<property name="mappingResources">
<list>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* action..*.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.sql.BatchUpdateException"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="java.sql.BatchUpdateException"/>
</tx:attributes>
</tx:advice>
</beans>
...全文
88 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告