Spring事务管理中@Transactional的参数配置

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

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

  所谓事务,就必须具备ACID特性,即原子性、一致性、隔离性和持久性,在Hibernate的实现中,需要我们编写代码来完成事务的控制工作。

	public static void main(String[] args) {
//		Configuration cfg = new Configuration();
//		cfg.configure();
//		SessionFactory sf = cfg.buildSessionFactory();
//因为在HibernateUtil类中已经有一部分封装工作,所以以上三行注释掉了。
		Session s = null;
		Transaction tx = null;
		try {
			Class.forName("com.HibernateUtil");
			s = HibernateUtil.getSession();
			tx = s.beginTransaction(); //这里开启了事务
			
			//事务中所需的一系列数据库操作。
			
			tx.commit();
		} catch (HibernateException e) { //如果出现异常,且事务已经开启,则需要回滚。
			if (tx != null)
				tx.rollback();
			throw e;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally { //无论如何都需要关闭Session。
			if (s != null)
				s.close();
		}
		//sf.close();
		System.out.println("end");
	}

  上面的代码大致就是事务控制的一般思路,那么,由于此事务的管理具有一定的共性,我们就更倾向于使用Spring帮助我们来完成事务管理工作,具体配置方式不是本文的重点,大家可以查看其他文章。有以下两点需要重点注意:

 

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

 

  本文主要讲使用注解方式配置事务管理时@Transactional的各种参数配置问题。

  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[]类型,默认为空数组。

  最后四个参数都与回滚有关,首先,一般不推荐使用rollbackForClassName和noRollbackForClassName两个参数,而用另外两个参数来代替,从参数的类型上就可以看出区别,使用字符串的缺点在于:如果不是用类的完整路径,就可能导致回滚设置对位于不同包中的同名类都生效;且如果类名写错,也无法得到IDE的动态提示。

  但是,如果不配置任何与回滚有关的参数,不代表事务不会进行回滚,如果没有配置这四个选项,那么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