使用Spring自动Hibernate事务管理? [英] Automatic Hibernate Transaction Management with Spring?

查看:105
本文介绍了使用Spring自动Hibernate事务管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring框架在处理事务方面有多远?我通过其示例创建DAO方法,不用担心会话和事务管理,只需在XML中设置会话工厂和事务模板,然后将它们连接到您的DAO中,即可阅读Spring In Action建议。另一方面,SpringSource.org的文档表明需要大量的XML和/或注释才能实现这一目的。



这里的真相是什么,什么是最简单的我可以沿着

 从sessionfactory获取会话
打开事务
预制形式数据库操作
提交错误处理的交易

并使其成为

 瓶坯数据库操作

减少我在我的方法中使用的锅炉板事务代码的数量达到了最低限度? 解决方案

Spring提供至少3种交易划分方式:
$ b $ 1)通过TransactionTemplate或PlatformTransactionManager进行编程处理 - 在配置文件上点亮,但是非侵入式

2)声明通过XML - 详细的XML,但非侵入性的

<3>通过注释声明 - 轻量级XML,n ot入侵



您选择哪一个取决于哪一个最适合您的需求,Spring不会为您选择。从你的问题来看,这听起来像是注释方法。



我建议阅读Spring参考手册,注释驱动的事务处理部分。它清晰简洁。



我总是先咨询ref文档,如果它不在文档中,只能咨询一本书。


How far does the spring framework go with transaction handling? My reading of the book "Spring In Action" suggestions with its examples that you create DAO methods that don't worry about Session and Transaction management fairly simply by setting up a session factory and transaction template in XML and then wiring them into your DAO. SpringSource.org's documentation, on the other hand, suggests that need tons of XML and/or annotation to make this happen.

What is the truth here, what is the simplest way I can take code along the lines of

get session from sessionfactory
open transaction
preform database actions
commit transaction with error handling

and make it just

preform database actions

reducing the amount of boiler plate transactional code that I have across my methods to a minimum?

解决方案

Spring provides at least 3 ways of transaction demarcation:

1) Programmatic handling, via TransactionTemplate or PlatformTransactionManager - light on config, but invasive

2) Declarative via XML - verbose XML, but non-invasive

3) Declarative via annotations - light on XML, not invasive

Which one you pick depends on which one best suits your needs, Spring doesn't make that choice for you. From your question, it sounds like the annotation approach is what you're after.

I suggest reading the Spring reference manual, the section of annotation-driven transaction handling. It's clear and concise.

I always consult the ref docs first, and only consult a book if it's not in the docs.

这篇关于使用Spring自动Hibernate事务管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆