`
fujiejie
  • 浏览: 7933 次
  • 性别: Icon_minigender_1
  • 来自: 江西
社区版块
存档分类
最新评论

TransactionProxyFactoryBean代理事务

阅读更多
配置文件:
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的DTD信息 -->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!-- Spring配置文件的根元素 -->
<beans>
	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost/javaee"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="root"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="32147"/>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="40"/>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="20"/>
	</bean>
	<!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 -->
	<!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现-->
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 配置DataSourceTransactionManager时需要依注入DataSource的引用 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 配置一个业务逻辑Bean -->
	<bean id="test" class="lee.TestImpl">
		<property name="ds" ref="dataSource"/>
	</bean>
	<!-- 为业务逻辑Bean配置事务代理 -->
    <bean id="testTrans" class=
		"org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<!-- 为事务代理工厂Bean注入事务管理器 -->
		<property name="transactionManager" ref="transactionManager"/> 
		<property name="target" ref="test"/>
		<!-- 指定事务属性 -->
		<property name="transactionAttributes"> 
			<props> 
				<prop key="*">PROPAGATION_REQUIRED</prop> 
			</props>
		</property>
	</bean> 
</beans>

接口:
public interface Test
{
	public void insert(String u);
}

接口实现:
public class TestImpl implements Test
{
	private DataSource ds;
	public void setDs(DataSource ds)
	{
		this.ds = ds;
	}
	public void insert(String u)
	{
		JdbcTemplate jt = new JdbcTemplate(ds);
		jt.execute("insert into mytable values('" + u + "')");
		//两次插入相同的数据,将违反主键约束
		jt.execute("insert into mytable values('" + u + "')");
		//如果增加事务控制,我们发现第一条记录也插不进去。
		//如果没有事务控制,则第一条记录可以被插入
	}
}


测试:
public class MainTest
{
	public static void main(String[] args) 
	{
		//创建Spring容器
		ApplicationContext ctx = new 
			ClassPathXmlApplicationContext("bean.xml");
		//获取事务代理Bean
		Test t = (Test)ctx.getBean("testTrans");
		//执行插入操作
		t.insert("bbb");
	}
}
分享到:
评论
1 楼 t_tianxingjian 2014-05-31  
你好,我在使用事务代理的时候测试没生效。帮忙看看哪里有问题。谢谢
<!-- 数据库操作类bean -->
	<bean id="baseService" class="spring.db.impl.BaseServiceImpl">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

	<!-- 配置JDBC事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

	<!-- 配置事件代理工厂 -->
	<bean id="transactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<!-- 要依赖事务管理器 -->
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>

		<!-- 接口代理方式 -->
		<property name="proxyInterfaces">
			<list>
				<value>spring.db.Inter.BaseService</value>
			</list>
		</property>

		<!-- 要依赖目标对象 -->
		<property name="target">
			<ref bean="baseService" />
		</property>

		<!-- 要依赖代理方式 -->
		<!-- <property name="proxyTargetClass" value="true"></property> -->

		<!-- 要依赖事务属性 -->
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
				<prop key="query">PROPAGATION_SUPPORTS,readOnly</prop>
				<prop key="insert">PROPAGATION_REQUIRED</prop>
				<prop key="delete">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>


package spring.db.impl;

import java.sql.Connection;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;

import spring.db.Inter.BaseService;

public class BaseServiceImpl implements BaseService {

	DataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 事务声明只读,这应该插入不了数据
	 */
	@Override
	public Object query() throws Exception {

		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)"
				+ "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')";

		JdbcTemplate jt = new JdbcTemplate(dataSource);
		jt.execute(sql);
		return null;
	}

	/**
	 * 事务方法抛异常,应该回滚
	 */
	@Override
	public Integer insert() throws Exception {
		
		Connection con = DataSourceUtils.getConnection(dataSource);
		Statement state = con.createStatement();

		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)"
				+ "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')";
		state.execute(sql);
		state.execute(sql);
		DataSourceUtils.doReleaseConnection(con, dataSource);
		if (true) {
			throw new Exception("insert 事务测试!");
		}
		return null;
	}

	@Override
	public Integer delete() throws Exception {
		
//		Connection con = dataSource.getConnection();
//		Statement state = con.createStatement();

		String sql = "delete from  sm_user where user_code = 'a' or user_code = 'b'";
//		state.execute(sql);

//		if (true) {
//			throw new Exception("delete 事务测试!");
//		}
		JdbcTemplate jt = new JdbcTemplate(dataSource);
		jt.execute(sql);
		
		return null;
	}

}

相关推荐

    spring 事务管理例子(TransactionProxyFactoryBean代理机制 和 tx/aop)

    适用企业系统的sturts1.2+spring2.0+jdbc 的架构。 通过TransactionProxyFactoryBean 代理机制和tx/aop两种方式来管理事务的详细demo

    Spring使用TransactionProxyFactoryBean声明式事务配置实例

    Spring使用TransactionProxyFactoryBean声明式事务配置实例

    spring基于AOP实现事务

    演示了spring基于AOP代理TransactionProxyFactoryBean实现事务

    Spring实战之使用TransactionProxyFactoryBean实现声明式事务操作示例

    主要介绍了Spring实战之使用TransactionProxyFactoryBean实现声明式事务操作,结合实例形式分析了spring使用TransactionProxyFactoryBean实现声明式事务相关配置、接口设置与使用技巧,需要的朋友可以参考下

    Spring配置事务在DAO层和业务逻辑层

    Spring 通过AOP 实现声明式事务管理。通常通过TransactionProxyFactoryBean 设置Spring 事务代理

    Spring+Hibernate事务管理

    * 需要为每个进行事务管理的类,配置一个TransactionProxyFactoryBean进行增强. * 基于AspectJ的XML方式.(经常使用) * 一旦配置好之后,类上不需要添加任何东西 * 基于注解方式.(经常使用) * 配置简单,需要在业务...

    Spring事务配置

    Spring烦人配置文件中的事务配置.包括编程方法和声明式事务的三种方法(注解,acpectJ,transactionProxyFactoryBean)

    spring in action英文版

     5.6.2 自动代理事务  5.7 小结  第6章 远程调用  6.1 Spring远程调用概览  6.2 与RMI一起工作  6.2.1 连接RMI服务  6.2.2 输出RMI服务  6.3 使用Hessian和Burlap的远程调用  6.3.1 访问...

    spring-transaction

    需要为每个进行事务管理的类,配置一个TransactionProxyFactoryBean进行增强. 基于AspectJ的XML方式.(经常使用) 一旦配置好之后,类上不需要添加任何东西 基于注解方式.(经常使用) 配置简单,需要在业务层上添加一个@...

    Spring.3.x企业应用开发实战(完整版).part2

    TransactionProxyFactoryBean 9.5.3 基于tx/aop命名空间的配置 9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 ...

    Spring3.x企业应用开发实战(完整版) part1

    TransactionProxyFactoryBean 9.5.3 基于tx/aop命名空间的配置 9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 ...

    ssh(structs,spring,hibernate)框架中的上传下载

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    SPRING API 2.0.CHM

    All Classes AbstractAdvisorAutoProxyCreator AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory....

Global site tag (gtag.js) - Google Analytics