deltamaster posted @ Aug 06, 2011 08:31:48 AM in Java EE with tags Hibernate spring Java EE Transactional 事务管理 , 11523 阅读

  Spring作为低侵入的Java EE框架之一,能够很好地与其他框架进行整合,其中Spring与Hibernate的整合实现的事务管理是常用的一种功能。


	public static void main(String[] args) {
//		Configuration cfg = new Configuration();
//		cfg.configure();
//		SessionFactory sf = cfg.buildSessionFactory();
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtil.getSession();
			tx = s.beginTransaction(); //这里开启了事务
		} catch (HibernateException e) { //如果出现异常,且事务已经开启,则需要回滚。
			if (tx != null)
			throw e;
		} catch (ClassNotFoundException e) {
		} finally { //无论如何都需要关闭Session。
			if (s != null)



  1. @Transactional注解就代表支持事务管理,如果这个注解在类上,那么表示该注解对于所有该类中的public方法都生效;如果注解出现在方法上,则代表该注解仅对该方法有效,会覆盖先前从类层次继承下来的注解。
  2. 一般情况下不要将这个注解加到接口和抽象类上,因为注解是不能被继承的。



  1. propagation参数,Propagation类型(枚举),默认值为Propogation.REQUIRED,支持的值有REQUIRED、MANDATORY、NESTED、NEVER、NOT_SUPPORTED、REQUIRE_NEW、SUPPORTS。关于这个问题的详细说明将在以后的文章中展开。
  2. isolation参数,Isolation类型(枚举),默认值为Isolation.DEFAULT,支持的值有DEFAULT、READ_COMMITTED、READ_UNCOMMITTED、REPEATABLE_READ、SERIALIZABLE。关于这个问题的详细说明将在以后的文章中展开。
  3. timeout参数,int类型,事务的超时时间,默认值为-1,即不会超时。
  4. readOnly参数,boolean类型,true表示事务为只读,默认值为false。
  5. rollbackFor参数,Class<? extends Throwable>[]类型,默认为空数组。
  6. rollbackForClassName参数,String[]类型,默认为空数组。
  7. noRollbackFor参数,Class<? extends Throwable>[]类型,默认为空数组。
  8. noRollbackForClassName参数,String[]类型,默认为空数组。


  但是,如果不配置任何与回滚有关的参数,不代表事务不会进行回滚,如果没有配置这四个选项,那么DefaultTransactionAttribute配置将会生效,具体的行为是,抛掷任何unchecked Exception都会触发回滚,当然包括所有的RuntimeException。

* 本文在CC BY-SA(署名-相同方式共享)协议下发布。
JSC Result Comilla B 说:
Sep 03, 2022 12:45:12 AM

Comilla board is another education board working under Secondary and Higher Secondary Education, Bangladesh, and the education board is also successfully completed the Grade 8 terminal examinations at all selected examination test centers at Comilla division, and the Junior School Certificate and Junior Dakhil Certificate terminal examination is a second largest examination in the country. <a href="https://bdjscresult2018.com/jsc-result-comilla-board-with-marksheet/">JSC Result Comilla Board</a> The Comilla Board is also completed those subject wise exams between 1st to 3rd week of November as per schedule announced by School Education department, and there are a huge number of students are participated like as all other educational boards from all districts of the division, right now they are waiting to check JSC & JDC Result 2022 Comilla Board with subject wise marks and total marksheet with final CGPA grade of the student.

charlly 说:
Dec 29, 2022 12:19:50 PM

Because Java EE is convention-over-configuration oriented, real-world apps don't require a lot of configuration. When used with Java 8, Java EE 7 has sufficient functionality to handle most use cases without any additional requirements. Given that the deployment implementation is already built into the runtime, Java EE is the ideal platform for container technologies like Docker. If at all possible, Sebastian advises separating business logic from the runtime. diamonds rings for cheap To do this, the programme must be contained in a WAR file that solely contains its code.

登录 *

loading captcha image...
or Ctrl+Enter