码迷,mamicode.com
首页 > 其他好文 > 详细

转TransactionProxyFactoryBean代理事务

时间:2014-10-04 16:37:26      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   使用   ar   

<?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");
    }
}

 

转TransactionProxyFactoryBean代理事务

标签:des   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/zuiyirenjian/p/4006017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!