spring中hibernate的事务回滚
项目架子:servlet+spring+hibernate+oracle
那个Action大致是这样的,这里面涉及到两个数据的插入,一个是merchantDAO.save(merchant);一个是userDAO.save(user);我想用spring事务控制,如果user插入失败,则之前插入的merchant回滚,我试了试,没实现,估计是我配置文件没写对。
配置文件代码大致如下:
那个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>
...全文
相关推荐
带事务回滚测试的SSH整合小案例(Spring 3.2+Hibernate 3.3+Struts 2.3)
集成了Spring 3.2.2 Hibernate 3.3.2 Struts 2.3.4 用JPA的注解实现orm 不用创建数据库表 只需本机上有oracle即可 用JSR注解进行Spring的依赖注入 用Spring Test整合Junit4 进行测试并自动事务回滚 不对数据库造成持久化操作
spring mvc+hibernate 实现事务管理(全注解版)
spring mvc hibernate 实现事务管理 jar包管理用maven,启动服务器用jetty,里边还有memcached数据库,但是本例子没有用到。
Spring Hibernate 事务处理 详细说明
Spring Hibernate 事务处理 详细说明 Spring Hibernate 事务处理 详细说明
Spring AOP管理Hibernate事务(TransactionInSpringAOP)
NULL 博文链接:https://javazeke.iteye.com/blog/450959
spring hibernate mysql 事务实例
一个简单的spring+hibernate+mysql 事务实例


Web 开发
