`
kissroom112
  • 浏览: 30430 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring中编程式事务处理(使用TransactionTemplate)

阅读更多
Spring的编程式事务处理,需要使用Hibernate事务回调接口,事务回调接口可以管理Hibernate的事务:

TransactionCallbackWithoutResult —— 执行事务没有返回值,例如save、update、delete等等;

TransactionCallback —— 执行事务处理后有返回值,如find要返回结果集(List);

spring相关配置
<!-- dataSource -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@192.168.26.188:1521:xxx" />
		<property name="username" value="xx" />
		<property name="password" value="xx" />

		<!-- Connection Pooling Info -->
		<property name="initialSize" value="3" />
		<property name="maxActive" value="5" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="1000" />
		<property name="poolPreparedStatements" value="true" />
		<property name="defaultAutoCommit" value="true" />			
	</bean>

	<bean id="dataSourceTransactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<constructor-arg ref="dataSource" />
	</bean>	

	<bean id="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<constructor-arg ref="dataSourceTransactionManager" />
	</bean>


public void createPerson(final Person person) {
   transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
   transactionTemplate.execute(new TransactionCallbackWithoutResult(){    // 使用无返回值的事务回调接口
    @Override
    protected void doInTransactionWithoutResult(TransactionStatus arg0) {
     getHibernateTemplate().save(person);    
    }   
   });
}

public Person queryOnePerson(final String hql) {
   transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
   return (Person)transactionTemplate.execute(new TransactionCallback(){     // 使用带返回值的事务回调接口

    public Object doInTransaction(TransactionStatus arg0) {
     return getHibernateTemplate().find(hql).get(0);
    }   
   });
}
分享到:
评论

相关推荐

    spring编程式事务实现

    演示了spring编程式事务的实现,通过TransactionTemplate模板进行事务控制

    spring_tx编程式事务代码

    Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可

    Spring事务原理、Spring事务配置的五种方式

    编程式主要使用TransactionTemplate。 void add() { transactionTemplate.execute( new TransactionCallback(){ pulic Object doInTransaction(TransactionStatus ts) { //do sth} } } 声明式的比编程式的更灵活。...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 9.8.1. BEA WebLogic 9.8.2. IBM WebSphere 9.9. 公共问题的解决方案 9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的...

    Spring中文帮助文档

    9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 9.8.1. IBM WebSphere 9.8.2. BEA WebLogic 9.8.3. Oracle OC4J 9.9. 常见问题的解决方法 9.9.1. 对一个特定的 DataSource 使用了错误...

    springMVC + Hibernate 工程模板

    和transactionTemplate(用于编程式事务处理,只用于特殊需要,因为已经存在配置式事务,一般符合命名的方法会自动创建事务) 其他功能: shown工具包 - 图片上传,分页 urlRewrite - 访问地址重定向,用于页面伪...

    Spring 2.0 开发参考手册

    9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 9.8.1. BEA WebLogic 9.8.2. IBM WebSphere 9.9. 公共问题的解决方案 9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多...

    Spring API

    9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 9.8.1. IBM WebSphere 9.8.2. BEA WebLogic 9.8.3. Oracle OC4J 9.9. 常见问题的解决方法 9.9.1. 对一个特定的 DataSource 使用了错误...

    spring chm文档

    9.7. 选择编程式事务管理还是声明式事务管理 9.8. 与特定应用服务器集成 9.8.1. BEA WebLogic 9.8.2. IBM WebSphere 9.9. 公共问题的解决方案 9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多...

    spring.net中文手册在线版

    14.5.5.通过TransactionProxyFactoryObject使用声明式事务 14.5.6. 通过ProxyFactoryObject使用声明式事务 14.5.7. Using Abstract object definitions 14.5.8. Declarative Transactions using ProxyFactoryObject...

Global site tag (gtag.js) - Google Analytics