浏览 1742 次
锁定老帖子 主题:spring 事务笔记(二)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-07-20
TransactionProxyFactoryBean 实现在无侵入的事务 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="agsfd"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="mappingResources"> <value>hibernate/A.hbm.xml</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> </bean> <!-- --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="testDao" class="test1.TestDao"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate" /> </property> </bean> <bean id="managerTarget" class="test1.ManagerTarget"> <property name="testDao"> <ref bean="testDao" /> </property> </bean> <bean id="manager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="target"> <ref bean="managerTarget" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">//实现事务的方法 PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop> </props> </property> </bean> </beans> package test1; import hibernate.A; public class ManagerTarget { ITestDao testDao; public ITestDao getTestDao() { return testDao; } public void setTestDao(ITestDao testDao) { this.testDao = testDao; } public void insert(){ A ab = new A(new Integer(121),"c++"); A abc = new A(new Integer(130),"java"); testDao.insert(ab); testDao.insert(abc); } } package test1; import hibernate.A; public interface ITestDao { public void insert(A a); } import hibernate.A; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import test1.ITestDao; public class TestDao extends HibernateDaoSupport implements ITestDao { public void insert(A a) { getHibernateTemplate().save(a); } } package test1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import test.ProgrammaticManager; public class Test { public static void main(String[] args) { ApplicationContext acx = new ClassPathXmlApplicationContext( "file:src/beans.xml"); // TransactionManager transaction = (TransactionManager) acx // .getBean("transactionManager"); ManagerTarget pm= (ManagerTarget) acx.getBean("manager"); pm.insert(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |