TransactionProxyFactoryBean代理事务

来源:互联网 发布:竞猜源码 编辑:程序博客网 时间:2024/05/16 11:52
  1. (转载别人的用于学习和记录)
  2. <?xml version="1.0" encoding="GBK"?>  
  3. <!-- 指定Spring配置文件的DTD信息 -->  
  4. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  5.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  6. <!-- Spring配置文件的根元素 -->  
  7. <beans>  
  8.     <!-- 定义数据源Bean,使用C3P0数据源实现 -->  
  9.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  10.         destroy-method="close">  
  11.         <!-- 指定连接数据库的驱动 -->  
  12.         <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
  13.         <!-- 指定连接数据库的URL -->  
  14.         <property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"/>  
  15.         <!-- 指定连接数据库的用户名 -->  
  16.         <property name="user" value="root"/>  
  17.         <!-- 指定连接数据库的密码 -->  
  18.         <property name="password" value="32147"/>  
  19.         <!-- 指定连接数据库连接池的最大连接数 -->  
  20.         <property name="maxPoolSize" value="40"/>  
  21.         <!-- 指定连接数据库连接池的最小连接数 -->  
  22.         <property name="minPoolSize" value="1"/>  
  23.         <!-- 指定连接数据库连接池的初始化连接数 -->  
  24.         <property name="initialPoolSize" value="1"/>  
  25.         <!-- 指定连接数据库连接池的连接的最大空闲时间 -->  
  26.         <property name="maxIdleTime" value="20"/>  
  27.     </bean>  
  28.     <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 -->  
  29.     <!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->  
  30.     <bean id="transactionManager"   
  31.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  32.         <!-- 配置DataSourceTransactionManager时需要依注入DataSource的引用 -->  
  33.         <property name="dataSource" ref="dataSource"/>  
  34.     </bean>  
  35.     <!-- 配置一个业务逻辑Bean -->  
  36.     <bean id="test" class="lee.TestImpl">  
  37.         <property name="ds" ref="dataSource"/>  
  38.     </bean>  
  39.     <!-- 为业务逻辑Bean配置事务代理 -->  
  40.     <bean id="testTrans" class=  
  41.         "org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  42.         <!-- 为事务代理工厂Bean注入事务管理器 -->  
  43.         <property name="transactionManager" ref="transactionManager"/>   
  44.         <property name="target" ref="test"/>  
  45.         <!-- 指定事务属性 -->  
  46.         <property name="transactionAttributes">   
  47.             <props>   
  48.                 <prop key="*">PROPAGATION_REQUIRED</prop>   
  49.             </props>  
  50.         </property>  
  51.     </bean>   
  52. </beans>  

接口: 
Java代码  收藏代码
  1. public interface Test  
  2. {  
  3.     public void insert(String u);  
  4. }  

接口实现: 
Java代码  收藏代码
  1. public class TestImpl implements Test  
  2. {  
  3.     private DataSource ds;  
  4.     public void setDs(DataSource ds)  
  5.     {  
  6.         this.ds = ds;  
  7.     }  
  8.     public void insert(String u)  
  9.     {  
  10.         JdbcTemplate jt = new JdbcTemplate(ds);  
  11.         jt.execute("insert into mytable values('" + u + "')");  
  12.         //两次插入相同的数据,将违反主键约束  
  13.         jt.execute("insert into mytable values('" + u + "')");  
  14.         //如果增加事务控制,我们发现第一条记录也插不进去。  
  15.         //如果没有事务控制,则第一条记录可以被插入  
  16.     }  
  17. }  


测试: 
Java代码  收藏代码
  1. public class MainTest  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         //创建Spring容器  
  6.         ApplicationContext ctx = new   
  7.             ClassPathXmlApplicationContext("bean.xml");  
  8.         //获取事务代理Bean  
  9.         Test t = (Test)ctx.getBean("testTrans");  
  10.         //执行插入操作  
  11.         t.insert("bbb");  
  12.     }  
  13. }  
分享到:  
Spring 2.X事务配置策略 | 声明式事务
  • 2009-09-18 19:58
  • 浏览 2225
  • 评论(1)
  • 相关推荐
评论
1 楼 t_tianxingjian 2014-05-31  
你好,我在使用事务代理的时候测试没生效。帮忙看看哪里有问题。谢谢
Java代码  收藏代码
  1. <!-- 数据库操作类bean -->  
  2.     <bean id="baseService" class="spring.db.impl.BaseServiceImpl">  
  3.         <property name="dataSource">  
  4.             <ref bean="dataSource" />  
  5.         </property>  
  6.     </bean>  
  7.   
  8.     <!-- 配置JDBC事务管理器 -->  
  9.     <bean id="transactionManager"  
  10.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  11.         <property name="dataSource">  
  12.             <ref bean="dataSource" />  
  13.         </property>  
  14.     </bean>  
  15.   
  16.     <!-- 配置事件代理工厂 -->  
  17.     <bean id="transactionProxy"  
  18.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  19.         <!-- 要依赖事务管理器 -->  
  20.         <property name="transactionManager">  
  21.             <ref bean="transactionManager" />  
  22.         </property>  
  23.   
  24.         <!-- 接口代理方式 -->  
  25.         <property name="proxyInterfaces">  
  26.             <list>  
  27.                 <value>spring.db.Inter.BaseService</value>  
  28.             </list>  
  29.         </property>  
  30.   
  31.         <!-- 要依赖目标对象 -->  
  32.         <property name="target">  
  33.             <ref bean="baseService" />  
  34.         </property>  
  35.   
  36.         <!-- 要依赖代理方式 -->  
  37.         <!-- <property name="proxyTargetClass" value="true"></property> -->  
  38.   
  39.         <!-- 要依赖事务属性 -->  
  40.         <property name="transactionAttributes">  
  41.             <props>  
  42.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
  43.                 <prop key="query">PROPAGATION_SUPPORTS,readOnly</prop>  
  44.                 <prop key="insert">PROPAGATION_REQUIRED</prop>  
  45.                 <prop key="delete">PROPAGATION_REQUIRED</prop>  
  46.             </props>  
  47.         </property>  
  48.     </bean>  


Java代码  收藏代码
  1. package spring.db.impl;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.Statement;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import org.springframework.jdbc.core.JdbcTemplate;  
  9. import org.springframework.jdbc.datasource.DataSourceUtils;  
  10.   
  11. import spring.db.Inter.BaseService;  
  12.   
  13. public class BaseServiceImpl implements BaseService {  
  14.   
  15.     DataSource dataSource;  
  16.   
  17.     public DataSource getDataSource() {  
  18.         return dataSource;  
  19.     }  
  20.   
  21.     public void setDataSource(DataSource dataSource) {  
  22.         this.dataSource = dataSource;  
  23.     }  
  24.   
  25.     /** 
  26.      * 事务声明只读,这应该插入不了数据 
  27.      */  
  28.     @Override  
  29.     public Object query() throws Exception {  
  30.   
  31.         String sql = "insert into sm_user (ABLE_TIME, AUTHEN_TYPE, CUSERID, DISABLE_TIME, DR, ISCA, KEYUSER, LANGCODE, LOCKED_TAG, PK_CORP, PWDLEVELCODE, PWDPARAM, PWDTYPE, TS, USER_CODE, USER_NAME, USER_NOTE, USER_PASSWORD)"  
  32.                 + "values ('2014-05-28', 'staticpwd', 'baidAA10000000000361', '', 0, 'N', '', 'simpchn', 'N', '0001', 'update', '2014-05-28', 0, '2014-05-28 17:51:26', 'a', 'a', '', 'jlehfdffcfmohiag')";  
  33.   
  34.         JdbcTemplate jt = new JdbcTemplate(dataSource);  
  35.         jt.execute(sql);  
  36.         return null;  
  37.     }  
  38.   
  39.     /** 
  40.      * 事务方法抛异常,应该回滚 
  41.      */  
  42.     @Override  
  43.     public Integer insert() throws Exception {  
  44.           
  45.         Connection con = DataSourceUtils.getConnection(dataSource);  
  46.         Statement state = con.createStatement();  
  47.   
  48.         String sql = "insert into sm_user (ABLE_TIME, AUTHEN_TYPE, CUSERID, DISABLE_TIME, DR, ISCA, KEYUSER, LANGCODE, LOCKED_TAG, PK_CORP, PWDLEVELCODE, PWDPARAM, PWDTYPE, TS, USER_CODE, USER_NAME, USER_NOTE, USER_PASSWORD)"  
  49.                 + "values ('2014-05-28', 'staticpwd', 'baiaAA10000000000361', '', 0, 'N', '', 'simpchn', 'N', '0001', 'update', '2014-05-28', 0, '2014-05-28 17:51:26', 'b', 'b', '', 'jlehfdffcfmohiag')";  
  50.         state.execute(sql);  
  51.         state.execute(sql);  
  52.         DataSourceUtils.doReleaseConnection(con, dataSource);  
  53.         if (true) {  
  54.             throw new Exception("insert 事务测试!");  
  55.         }  
  56.         return null;  
  57.     }  
  58.   
  59.     @Override  
  60.     public Integer delete() throws Exception {  
  61.           
  62. //      Connection con = dataSource.getConnection();  
  63. //      Statement state = con.createStatement();  
  64.   
  65.         String sql = "delete from  sm_user where user_code = 'a' or user_code = 'b'";  
  66. //      state.execute(sql);  
  67.   
  68. //      if (true) {  
  69. //          throw new Exception("delete 事务测试!");  
  70. //      }  
  71.         JdbcTemplate jt = new JdbcTemplate(dataSource);  
  72.         jt.execute(sql);  
  73.           
  74.         return null;  
  75.     }  
  76.   
  77. }  
0 0
原创粉丝点击