67,512
社区成员
发帖
与我相关
我的任务
分享
<!--为业务逻辑Bean配置事务代理 -->
<bean id="testTrans" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="test"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="superDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="tranMager"/>
1.2事务管理规则
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="bookBizPro" parent="superDao">
<property name="target" ref="bookBiz"></property>
</bean>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--定義數據源Bean,使用C3P0數據源實現-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost/test"/>
<property name="user" value="root"/>
<property name="password" value="1234"/>
<property name="maxPoolSize" value="40"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
</bean>
<!--配置JDBC數據源的局部事務管理器,使用DataSourceTrannsactionManager類 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="test" class="spring.TestImpl">
<property name="ds" ref="dataSource"/>
</bean>
<!--配置一个业务逻辑组件 -->
<bean id="myService" class="spring.MyServiceImpl">
<!--将DAO组件注入给业务逻辑组件-->
<property name="test" ref="test"/>
</bean>
<!--為業務邏輯Bean配置事務代理 -->
<bean id="testTrans" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="myService"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
package spring;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
public class TestImpl implements Test {
private DataSource ds;
public void setDs(DataSource ds) {
this.ds = ds;
}
public void insert(int i) {
// TODO Auto-generated method stub
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("insert into mytable value("+i+")");
}
}
package spring;
public class MyServiceImpl implements MyService{
private Test test;
public void setTest(Test test) {
this.test = test;
}
@Override
public void insert(int id) {
// TODO Auto-generated method stub
test.insert(id);
test.insert(id);
}
}
package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String arg[])throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
MyService t = (MyService)ctx.getBean("testTrans");
t.insert(1);
}
}