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

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

问题描述

spring 框架在事务处理方面走了多远?我对Spring In Action"一书的阅读建议及其示例,您可以通过在 XML 中设置会话工厂和事务模板,然后将它们连接到您的 DAO 中,来创建无需担心会话和事务管理的 DAO 方法.另一方面,SpringSource.org 的文档表明需要大量 XML 和/或注释来实现这一点.

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

让它变得简单

preform database actions

将我的方法中的样板事务代码量减少到最低限度?

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

推荐答案

Spring 提供了至少 3 种事务划分方式:

Spring provides at least 3 ways of transaction demarcation:

1) 编程处理,通过 TransactionTemplate 或 PlatformTransactionManager - 配置简单,但具有侵入性

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

2) 通过 XML 声明 - 冗长的 XML,但非侵入性

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

3) 通过注释进行声明 - 对 XML 的关注,而不是侵入性

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

您选择哪一个取决于哪一个最适合您的需求,Spring 不会为您做出这样的选择.从您的问题来看,您所追求的似乎是注释方法.

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.

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

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天全站免登陆