Spring实现iBATIS事务回滚

转载 2012年03月28日 22:45:27

配置如下:

Spring实现iBATIS事务回滚之Web.xml

  1. ﹤?xml version="1.0" encoding="UTF-8"?﹥
  2. ﹤web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  5.      ﹤context-param﹥
  6.         ﹤param-name﹥contextConfigLocation﹤/param-name﹥
  7.         ﹤param-value﹥classpath:applicationContext.xml﹤/param-value﹥
  8.      ﹤/context-param﹥
  9.      ﹤listener﹥
  10.         ﹤listener-class﹥
  11.             org.springframework.web.context.ContextLoaderListener
  12.         ﹤/listener-class﹥
  13.      ﹤/listener﹥
  14.      ﹤servlet﹥
  15.         ﹤servlet-name﹥AppConfigServlet﹤/servlet-name﹥
  16.         ﹤servlet-class﹥com.test.ConfigServlet﹤/servlet-class﹥
  17.         ﹤load-on-startup﹥2﹤/load-on-startup﹥
  18.      ﹤/servlet﹥
  19.      ﹤welcome-file-list﹥
  20.         ﹤welcome-file﹥index.jsp﹤/welcome-file﹥
  21.      ﹤/welcome-file-list﹥
  22. ﹤/web-app﹥

Spring实现iBATIS事务回滚之ApplicationContext.xml

  1. ﹤bean id="transactionManager"
  2. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
  3.         ﹤property name="dataSource" ref="dataSource" /﹥
  4.      ﹤/bean﹥
  5. ﹤bean id="businessTarget"
  6.        class="com.test.BusinessInterfaceImpl" /﹥
  7.        
  8.      ﹤bean id="businessBean"
  9.        class="org.springframework.aop.framework.ProxyFactoryBean"
  10.         ﹤property name="singleton"
  11.             ﹤value﹥true﹤/value﹥
  12.         ﹤/property﹥
  13.         ﹤property name="proxyInterfaces"
  14.             ﹤value﹥com.test.BusinessInterface﹤/value﹥
  15.         ﹤/property﹥
  16.         ﹤property name="interceptorNames"
  17.             ﹤list﹥
  18.                ﹤value﹥transactionInterceptor﹤/value﹥
  19.                ﹤value﹥businessTarget﹤/value﹥
  20.             ﹤/list﹥
  21.         ﹤/property﹥
  22.         ﹤property name="proxyTargetClass"﹥﹤value﹥true﹤/value﹥﹤/property﹥
  23.      ﹤/bean﹥
  24.     
  25.      ﹤bean id="transactionInterceptor"
  26.        class="org.springframework.transaction.interceptor.TransactionInterceptor"
  27.         ﹤property name="transactionManager"
  28.             ﹤ref local="transactionManager" /﹥
  29.         ﹤/property﹥
  30.         ﹤property name="transactionAttributes"
  31.             ﹤props﹥
  32.                ﹤prop key="*"﹥PROPAGATION_REQUIRED,-Exception﹤/prop﹥
  33.             ﹤/props﹥
  34.         ﹤/property﹥
  35.      ﹤/bean﹥
  36.      ﹤bean id="dataSource"
  37.        class="org.springframework.jndi.JndiObjectFactoryBean"
  38.         ﹤property name="jndiName"
  39.             ﹤value﹥gissys﹤/value﹥
  40.         ﹤/property﹥
  41.      ﹤/bean﹥
  42. ﹤bean id="sqlMapClient"
  43.        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
  44.         ﹤property name="configLocation"
  45.             ﹤value﹥
  46.                classpath:com/emaptech/persistence/SqlMapConfig.xml
  47.             ﹤/value﹥
  48.         ﹤/property﹥
  49.         ﹤property name="dataSource" ref="dataSource"﹥﹤/property﹥
  50.      ﹤/bean﹥

Spring实现iBATIS事务回滚之Ibatis.xml

  1. ﹤insert id="testInsert" parameterClass="string"
  2.     insert into corebase.cc select 6,#value# from dual
  3. ﹤/insert﹥
  4. ﹤insert id="testInsertWrong" parameterClass="string"
  5.     insert into corebase.cc select 6,#value#,7 from dual
  6. ﹤/insert﹥

Spring实现iBATIS事务回滚之ConfigServlet.java

  1. package com.test;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.web.context.support.WebApplicationContextUtils;
  6. public class ConfigServlet extends HttpServlet {
  7.     /**
  8.       * UID
  9.       */
  10.     private static final long serialVersionUID = 5118794568550751611L;
  11.     public void init() throws ServletException {
  12.         ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
  13.         BaseService.getInstance().init(ctx);
  14.        super.init();
  15.      }
  16. }

Spring实现iBATIS事务回滚之BaseService.java

  1. package com.test;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.ApplicationContextAware;
  6. import org.springframework.core.io.ClassPathResource;
  7. import com.ibatis.sqlmap.client.SqlMapClient;
  8. public class BaseService implements ApplicationContextAware {
  9.     private static ApplicationContext ctx;
  10.     private static SqlMapClient sqlMapClient;
  11.     private static SqlMapClient sqlMapClientProxy;
  12.     private static BusinessInterface businessBean;
  13.     
  14.     private static BaseService instance = new BaseService();
  15.   
  16.     /**
  17.       * @return 返回 静态类
  18.       */
  19.     public static BaseService getInstance(){
  20.        return instance;
  21.      }
  22.     /**
  23.       * @return 返回 初始化BaseService,存于内存
  24.       */
  25.     public void init(ApplicationContext ctx) {
  26.         setApplicationContext(ctx);
  27.         setSqlMapClient(ctx);
  28.         setBusinessBean(ctx);
  29. //      setSqlMapClientProxy(ctx);
  30.         System.out.println(" INFO - 初始化baseservice成功");
  31.      }
  32.     public void setSqlMapClient(ApplicationContext ctx){
  33.         sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
  34.      }    
  35.     /**
  36.       * @return 返回 sqlMapClient。
  37.       */
  38.     public SqlMapClient getInstanceSqlMapClient() {
  39.       return this.sqlMapClient;
  40.      }
  41.     /**
  42.       * 通过spring注入ApplicationContext
  43.       *
  44.       * @param ApplicationContext
  45.       * @return null
  46.       */
  47.     public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  48.         ctx = arg0;
  49.      }
  50.     public Object getBean(String beanName) {
  51.        return ctx.getBean(beanName);
  52.      }
  53.     /**
  54.       * @return 返回 sqlMapClient。
  55.       */
  56.     public SqlMapClient getSqlMapClient() {
  57.        return (SqlMapClient) ctx.getBean("sqlMapClient");
  58.      }
  59.     public void setSqlMapClientProxy(ApplicationContext ctx) {
  60.         sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");
  61.      }
  62.     public SqlMapClient getSqlMapClientProxy() {
  63.        return sqlMapClientProxy;
  64.      }
  65.     public static BusinessInterface getBusinessBean() {
  66.        return businessBean;
  67.      }
  68.     public static void setBusinessBean(ApplicationContext ctx) {
  69.         businessBean = (BusinessInterface) ctx.getBean("businessBean");
  70.      }
  71. }

Spring实现iBATIS事务回滚

配置如下: Spring实现iBATIS事务回滚之Web.xml ﹤?xml version="1.0" encoding="UTF-8"?﹥ ﹤web-app xmlns="http://...

Spring实现iBATIS事务回滚~

Spring 实现iBATIS事务回滚是怎么实现的呢?让我们开始这篇文章的学习,这篇文章将会想你介绍Spring 实现iBATIS事务回滚的相关消息 AD: Spr...
  • ffmoshu
  • ffmoshu
  • 2012年03月28日 22:30
  • 1485

spring代理iBATIS时事务不能回滚

kkkk如题! 废话不多说了,直接上配置,配置如下,各位看了便知! applicationContext.xml Xml代码   xml version="1....
  • ffmoshu
  • ffmoshu
  • 2012年03月28日 22:57
  • 1005

Spring/SpringMVC/MyBatis整合+事务回滚

  • 2016年02月24日 11:33
  • 25.61MB
  • 下载

项目开发-iBatis事务源码之事务提交和回滚

接上篇继续事务的提交和结束流程如下。    commitTransaction       SqlMapClientImpl的commitTransaction操作,类似startTransacti...

事务: spring+hibernate实现事务回滚及其他

转载自:  http://hi.baidu.com/beforedead/blog/item/75d9272ad44dba395343c1f8.html 对于J2EE 应用程序而言,事务...

Spring中@Transactional事务回滚(含实例详细讲解,附源码)

Spring中@Transactional事务回滚(含实例详细讲解,附源码) 2016/05/02 | 分类: 基础技术 | 2 条评论 | 标签: SPRING, 事务 分享...

spring mybatis事务回滚配置

1、配置mybatis数据源

spring整合Hibernate事务不能自动回滚

最近在研究spring整合hibernate的时候,spring里面的transaction默认是对RumtimeException进行自动回滚,但是验证了好几遍,依然能插入数据,然后查了一下资料,原...

Spring事务管理——回滚(rollback-for)控制

探讨Spring事务控制中,异常触发事务回滚原理。文章进行了6种情况下的Spring事务是否回滚。 以下代码都是基于Spring与Mybatis整合,使用Spring声明式事务配置事务方法。 ...
  • ljyhust
  • ljyhust
  • 2017年06月18日 15:24
  • 2425
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring实现iBATIS事务回滚
举报原因:
原因补充:

(最多只允许输入30个字)