自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾

開(kāi)發(fā) 后端
Spring 實(shí)現(xiàn)iBATIS事務(wù)回滾是怎么實(shí)現(xiàn)的呢?讓我們開(kāi)始這篇文章的學(xué)習(xí),這篇文章將會(huì)想你介紹Spring 實(shí)現(xiàn)iBATIS事務(wù)回滾的相關(guān)消息

Spring 實(shí)現(xiàn)ibatis事務(wù)回滾是怎么回事呢?

這兩天做一個(gè)網(wǎng)站需要在數(shù)據(jù)報(bào)錯(cuò)時(shí)自動(dòng)對(duì)數(shù)據(jù)進(jìn)行回滾,在此期間遇到一些問(wèn)題,現(xiàn)做下筆記

這里使用spring-Aop代理機(jī)制來(lái)實(shí)現(xiàn)事務(wù)的回滾:

配置如下:

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之Web.xml 

  1. ﹤?xml version="1.0" encoding="UTF-8"?﹥  
  2.  
  3. ﹤web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
  4.  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 
  6.  
  7.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"﹥  
  8.  
  9.     ﹤context-param﹥  
  10.  
  11.        ﹤param-name﹥contextConfigLocation﹤/param-name﹥  
  12.  
  13.        ﹤param-value﹥classpath:applicationContext.xml﹤/param-value﹥  
  14.  
  15.     ﹤/context-param﹥  
  16.  
  17.     ﹤listener﹥  
  18.  
  19.        ﹤listener-class﹥  
  20.  
  21.            org.springframework.web.context.ContextLoaderListener  
  22.  
  23.        ﹤/listener-class﹥  
  24.  
  25.     ﹤/listener﹥  
  26.  
  27.    
  28.  
  29.     ﹤servlet﹥  
  30.  
  31.        ﹤servlet-name﹥AppConfigServlet﹤/servlet-name﹥  
  32.  
  33.        ﹤servlet-class﹥com.test.ConfigServlet﹤/servlet-class﹥  
  34.  
  35.        ﹤load-on-startup﹥2﹤/load-on-startup﹥  
  36.  
  37.     ﹤/servlet﹥  
  38.  
  39.    
  40.  
  41.     ﹤welcome-file-list﹥  
  42.  
  43.        ﹤welcome-file﹥index.jsp﹤/welcome-file﹥  
  44.  
  45.     ﹤/welcome-file-list﹥  
  46.  
  47. ﹤/web-app﹥ 

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之ApplicationContext.xml

  1. ﹤bean id="transactionManager" 
  2.  
  3. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"﹥  
  4.  
  5.        ﹤property name="dataSource" ref="dataSource" /﹥  
  6.  
  7.     ﹤/bean﹥  
  8.  
  9. ﹤bean id="businessTarget" 
  10.  
  11.        class="com.test.BusinessInterfaceImpl" /﹥  
  12.  
  13.          
  14.  
  15.     ﹤bean id="businessBean" 
  16.  
  17.        class="org.springframework.aop.framework.ProxyFactoryBean"﹥  
  18.  
  19.        ﹤property name="singleton"﹥  
  20.  
  21.            ﹤value﹥true﹤/value﹥  
  22.  
  23.        ﹤/property﹥  
  24.  
  25.        ﹤property name="proxyInterfaces"﹥  
  26.  
  27.            ﹤value﹥com.test.BusinessInterface﹤/value﹥  
  28.  
  29.        ﹤/property﹥  
  30.  
  31.        ﹤property name="interceptorNames"﹥  
  32.  
  33.            ﹤list﹥  
  34.  
  35.               ﹤value﹥transactionInterceptor﹤/value﹥  
  36.  
  37.               ﹤value﹥businessTarget﹤/value﹥  
  38.  
  39.            ﹤/list﹥  
  40.  
  41.        ﹤/property﹥  
  42.  
  43.        ﹤property name="proxyTargetClass"﹥﹤value﹥true﹤/value﹥﹤/property﹥  
  44.  
  45.     ﹤/bean﹥  
  46.  
  47.       
  48.  
  49.     ﹤bean id="transactionInterceptor" 
  50.  
  51.        class="org.springframework.transaction.interceptor.TransactionInterceptor"﹥  
  52.  
  53.        ﹤property name="transactionManager"﹥  
  54.  
  55.            ﹤ref local="transactionManager" /﹥  
  56.  
  57.        ﹤/property﹥  
  58.  
  59.        ﹤property name="transactionAttributes"﹥  
  60.  
  61.            ﹤props﹥  
  62.  
  63.               ﹤prop key="*"﹥PROPAGATION_REQUIRED,-Exception﹤/prop﹥  
  64.  
  65.            ﹤/props﹥  
  66.  
  67.        ﹤/property﹥  
  68.  
  69.     ﹤/bean﹥  
  70.  
  71.     ﹤bean id="dataSource" 
  72.  
  73.        class="org.springframework.jndi.JndiObjectFactoryBean"﹥  
  74.  
  75.        ﹤property name="jndiName"﹥  
  76.  
  77.            ﹤value﹥gissys﹤/value﹥  
  78.  
  79.        ﹤/property﹥  
  80.  
  81.     ﹤/bean﹥  
  82.  
  83. ﹤bean id="sqlMapClient" 
  84.  
  85.        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"﹥  
  86.  
  87.        ﹤property name="configLocation"﹥  
  88.  
  89.            ﹤value﹥  
  90.  
  91.               classpath:com/emaptech/persistence/SqlMapConfig.xml  
  92.  
  93.            ﹤/value﹥  
  94.  
  95.        ﹤/property﹥  
  96.  
  97.        ﹤property name="dataSource" ref="dataSource"﹥﹤/property﹥  
  98.  
  99.     ﹤/bean﹥ 

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之Ibatis.xml

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

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之ConfigServlet.java

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

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之BaseService.java

  1. package com.test;  
  2.  
  3. import org.springframework.beans.BeansException;  
  4.  
  5. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  6.  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.ApplicationContextAware;  
  9.  
  10. import org.springframework.core.io.ClassPathResource;  
  11. import com.ibatis.sqlmap.client.SqlMapClient;  
  12.    
  13. public class BaseService implements ApplicationContextAware {  
  14.  
  15.     private static ApplicationContext ctx;  
  16.  
  17.     private static SqlMapClient sqlMapClient;  
  18.  
  19.     private static SqlMapClient sqlMapClientProxy;  
  20.  
  21.     private static BusinessInterface businessBean;  
  22.       
  23.     private static BaseService instance = new BaseService();  
  24.     
  25.  
  26.     /**  
  27.  
  28.      * @return 返回 靜態(tài)類  
  29.  
  30.      */ 
  31.  
  32.     public static BaseService getInstance(){  
  33.  
  34.        return instance;  
  35.  
  36.     }  
  37.  
  38.     /**  
  39.  
  40.      * @return 返回 初始化BaseService,存于內(nèi)存  
  41.  
  42.      */ 
  43.  
  44.     public void init(ApplicationContext ctx) {  
  45.  
  46.        setApplicationContext(ctx);  
  47.  
  48.        setSqlMapClient(ctx);  
  49.  
  50.        setBusinessBean(ctx);  
  51.  
  52. //     setSqlMapClientProxy(ctx);  
  53.  
  54.        System.out.println(" INFO - 初始化baseservice成功");  
  55.  
  56.     }  
  57.     public void setSqlMapClient(ApplicationContext ctx){  
  58.  
  59.        sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");  
  60.  
  61.     }      
  62.  
  63.     /**  
  64.  
  65.      * @return 返回 sqlMapClient。  
  66.  
  67.      */ 
  68.  
  69.     public SqlMapClient getInstanceSqlMapClient() {  
  70.       return this.sqlMapClient;  
  71.     }  
  72.  
  73.     /**  
  74.  
  75.      * 通過(guò)spring注入ApplicationContext  
  76.      *   
  77.      * @param ApplicationContext  
  78.  
  79.      * @return null  
  80.  
  81.      */ 
  82.  
  83.     public void setApplicationContext(ApplicationContext arg0) throws BeansException {  
  84.  
  85.        ctx = arg0;  
  86.  
  87.     }  
  88.  
  89.     public Object getBean(String beanName) {  
  90.  
  91.        return ctx.getBean(beanName);  
  92.  
  93.     }  
  94.     /**  
  95.  
  96.      * @return 返回 sqlMapClient。  
  97.  
  98.      */ 
  99.  
  100.     public SqlMapClient getSqlMapClient() {  
  101.  
  102.        return (SqlMapClient) ctx.getBean("sqlMapClient");  
  103.  
  104.     }  
  105.  
  106.     public void setSqlMapClientProxy(ApplicationContext ctx) {  
  107.  
  108.        sqlMapClientProxy= (SqlMapClient) ctx.getBean("sqlMapClientProxy");  
  109.  
  110.     }  
  111.     public SqlMapClient getSqlMapClientProxy() {  
  112.        return sqlMapClientProxy;  
  113.  
  114.     }  
  115.     public static BusinessInterface getBusinessBean() {  
  116.        return businessBean;  
  117.     }  
  118.  
  119.     public static void setBusinessBean(ApplicationContext ctx) {  
  120.  
  121.        businessBean = (BusinessInterface) ctx.getBean("businessBean");  
  122.  
  123.     }  
  124.  

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之BusinessInterface.java

  1. package com.test;  
  2. import java.sql.SQLException;  
  3.  
  4. public interface BusinessInterface {  
  5.  
  6.     public void hello() throws SQLException;  
  7.  

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之BusinessInterfaceImpl.java

  1. package com.test;  
  2.  
  3.    
  4.  
  5. import java.sql.SQLException;  
  6. import com.ibatis.sqlmap.client.SqlMapClient;  
  7.  
  8. public class BusinessInterfaceImpl implements BusinessInterface {  
  9.     public void hello() throws SQLException {  
  10.        System.out.println("hello Spring AOP.");  
  11.        SqlMapClient sqlMapClient = BaseService.getInstance().getInstanceSqlMapClient();  
  12.            sqlMapClient.startTransaction();  
  13.  
  14.            sqlMapClient.insert("testInsert","7");  
  15.  
  16.            System.out.println("1");  
  17.  
  18.            sqlMapClient.insert("testInsert","8");  
  19.  
  20.            System.out.println("2");  
  21.  
  22.            sqlMapClient.insert("testInsert","9");  
  23.  
  24.            System.out.println("3");  
  25.  
  26.            sqlMapClient.insert("testInsertWrong","10");  
  27.  
  28.            sqlMapClient.commitTransaction();   
  29.  
  30.     }  
  31.  

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾之測(cè)試頁(yè)面

  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%﹥  
  2.  
  3. ﹤%  
  4.  
  5. String path = request.getContextPath();  
  6.  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8.  
  9. %﹥  
  10.  
  11. ﹤!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"﹥  
  12.  
  13. ﹤html﹥  
  14.  
  15.   ﹤head﹥  
  16.  
  17.     ﹤base href="﹤%=basePath%﹥"﹥  
  18.  
  19.     ﹤title﹥My JSP 'index.jsp' starting page﹤/title﹥  
  20.  
  21.     ﹤meta http-equiv="pragma" content="no-cache"﹥  
  22.  
  23.     ﹤meta http-equiv="cache-control" content="no-cache"﹥  
  24.  
  25.     ﹤meta http-equiv="expires" content="0"﹥      
  26.  
  27.     ﹤meta http-equiv="keywords" content="keyword1,keyword2,keyword3"﹥  
  28.  
  29.     ﹤meta http-equiv="description" content="This is my page"﹥  
  30.  
  31.     ﹤!--  
  32.  
  33.     ﹤link rel="stylesheet" type="text/css" href="styles.css"﹥  
  34.  
  35.     --﹥  
  36.  
  37.   ﹤/head﹥  
  38.  
  39.   ﹤body﹥  
  40.  
  41.     This is my JSP page. ﹤br﹥  
  42.  
  43.   ﹤/body﹥  
  44.  
  45. ﹤/html﹥ 

Spring實(shí)現(xiàn)iBATIS事務(wù)回滾的相關(guān)信息就向你介紹到這里,希望對(duì)你了解Spring實(shí)現(xiàn)iBATIS事務(wù)回滾有所幫助。

【編輯推薦】

  1. Struts2.0+ibatis2.3完美整合實(shí)例之映射淺析
  2. Struts2.0+ibatis2.3整合實(shí)例實(shí)現(xiàn)服務(wù)層及Struts
  3. Struts2.0+ibatis2.3整合實(shí)例實(shí)現(xiàn)UI層淺析
  4. iBATIS入門(mén)程序六大步詳解
  5. iBATIS DAO事務(wù)淺析
責(zé)任編輯:仲衡 來(lái)源: yesky
相關(guān)推薦

2009-07-20 18:00:16

iBATIS DAO事

2020-11-18 10:16:52

數(shù)據(jù)庫(kù)回滾事務(wù)

2020-11-18 08:32:07

數(shù)據(jù)庫(kù)

2023-04-03 10:24:00

spring事務(wù)場(chǎng)景

2021-06-15 16:17:19

Commit報(bào)錯(cuò)事務(wù)

2009-07-15 17:41:55

iBATIS事務(wù)處理

2023-10-12 08:54:20

Spring事務(wù)設(shè)置

2009-11-16 17:15:12

Oracle減少回滾段

2009-11-16 13:41:18

Oracle分離回滾段

2017-05-18 16:07:23

回滾數(shù)據(jù)庫(kù)代碼

2009-06-29 17:54:47

Spring事務(wù)隔離

2011-01-27 10:11:46

J2EEjavaspring

2009-07-17 17:16:48

Spring iBAT

2012-05-29 16:25:45

Transaction

2020-08-19 09:45:29

Spring數(shù)據(jù)庫(kù)代碼

2009-09-25 12:59:53

Hibernate事務(wù)

2009-07-17 14:03:34

ibatis DAO事務(wù)管理

2018-11-16 15:35:10

Spring事務(wù)Java

2024-02-20 09:54:20

MySQL數(shù)據(jù)庫(kù)

2010-04-16 17:31:22

ORACLE回滾段
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)