论坛首页 Java版 Hibernate

getHibernateTemplate()处理事务的疑惑

浏览 6599 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2006-09-11
我在dao里面调用getHibernateTemplate().find("from XXXX"),那这里会启动一个事务.
那如果我又在service的一个方法里面调多个这样的dao,那会启动多次事务.
现在的问题是我在调service之前已经通过AOP启动了事务,这样的话会和service里面的事务重叠,这应该怎么解决的?是不是应该摒弃getHibernateTemplate()的自动开关事务功能?
   
最后更新时间:2006-09-11
你说你在AOP里启了一个connection来代理事务!
那service里的是什么事务?事务又不能嵌套,也不能分库!同一数据库连接的事务,怎么重叠啊?
除非你把AOP代理过后的connection再传回到service,让你的方法调用!
   
0 请登录后投票
最后更新时间:2006-09-11
check TransactionDefinition
	/**
	 * Support a current transaction, create a new one if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>This is typically the default setting of a transaction definition.
	 */
	int PROPAGATION_REQUIRED = 0;

	/**
	 * Support a current transaction, execute non-transactionally if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: For transaction managers with transaction synchronization,
	 * PROPAGATION_SUPPORTS is slightly different from no transaction at all,
	 * as it defines a transaction scopp that synchronization will apply for.
	 * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
	 * will be shared for the entire specified scope. Note that this depends on
	 * the actual synchronization configuration of the transaction manager.
	 * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
	 */
	int PROPAGATION_SUPPORTS = 1;

	/**
	 * Support a current transaction, throw an exception if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 */
	int PROPAGATION_MANDATORY = 2;

	/**
	 * Create a new transaction, suspend the current transaction if one exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: Actual transaction suspension will not work on out-of-the-box
	 * on all transaction managers. This in particular applies to JtaTransactionManager,
	 * which requires the <code>javax.transaction.TransactionManager</code> to be
	 * made available it to it (which is server-specific in standard J2EE).
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_REQUIRES_NEW = 3;

	/**
	 * Execute non-transactionally, suspend the current transaction if one exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: Actual transaction suspension will not work on out-of-the-box
	 * on all transaction managers. This in particular applies to JtaTransactionManager,
	 * which requires the <code>javax.transaction.TransactionManager</code> to be
	 * made available it to it (which is server-specific in standard J2EE).
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_NOT_SUPPORTED = 4;

	/**
	 * Execute non-transactionally, throw an exception if a transaction exists.
	 * Analogous to EJB transaction attribute of the same name.
	 */
	int PROPAGATION_NEVER = 5;

	/**
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
	 * <p>Note: Actual creation of a nested transaction will only work on specific
	 * transaction managers. Out of the box, this only applies to the JDBC
	 * DataSourceTransactionManager when working on a JDBC 3.0 driver.
	 * Some JTA providers might support nested transactions as well.
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
	 */
	int PROPAGATION_NESTED = 6;
   
0 请登录后投票
最后更新时间:2006-09-11
我对service的事务拦截是用PROPAGATION_REQUIRED的,那service里面调用了dao方法,而getHibernateTemplate()又是自动启动事务,这样不是有重叠吗?
有没有办法使getHibernateTemplate()不启动事务?
   
0 请登录后投票
最后更新时间:2006-09-11
wangx1949 写道
是不是应该摒弃getHibernateTemplate()的自动开关事务功能?


这句没看明白,自动开关事务功能是什么?

wangx1949 写道
我对service的事务拦截是用PROPAGATION_REQUIRED的,那service里面调用了dao方法,而getHibernateTemplate()又是自动启动事务,这样不是有重叠吗?

PROGAGATION_REQUIRED参见上面Feiing贴的源码:如果有一个事务的话,该方法会在该事务里运行。如果没有的话新建一个事务。
   
0 请登录后投票
最后更新时间:2006-09-11
难道是我理解有偏差,是不是我根本不用去考虑HibernateTemplate管不管理事务,我只要在serivce里做事务控制就可以了?
   
0 请登录后投票
最后更新时间:2006-09-11
事务是不可能重叠的,大哥!
从头到脚一个connection,怎么重叠啊?
你瑶重叠,也不过是同一个conn里,滚来滚去!有什么意思?
   
0 请登录后投票
最后更新时间:2006-09-11
不要总看申明式的事务,你自己会不会写事务?
事务是指一个connection下,多次更新数据库,而不提交,到最后全部更新正常后才提交!
其实不过是控制住了connection的commit罢了!
框架里的事务也是编出来的!
还有,申明事务,性能很差!灵活性很低!
一般有一个事务模版给你,已经很幸运了!
   
0 请登录后投票
最后更新时间:2006-09-11
...
public list findByName(name,value){
    return userDao.findBy(name,value);
}
...

假设有这样一段代码,我在application.xml里面用spring对这个方法声明事务,
但在userDao的findBy方法里面是用getHibernateTemplate().XXX来实现的,HibernateTemplate会自动开启事务,而我在调service里面的findByName之前也会开启一个事务,这样不是重叠了吗?
难道HibernateTemplate会自动去检测有没有开启事务?
   
0 请登录后投票
最后更新时间:2006-09-11
galaxystar 写道
事务是不可能重叠的,大哥!
从头到脚一个connection,怎么重叠啊?
你瑶重叠,也不过是同一个conn里,滚来滚去!有什么意思?

/**
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
	 * <p>Note: Actual creation of a nested transaction will only work on specific
	 * transaction managers. Out of the box, this only applies to the JDBC
	 * DataSourceTransactionManager when working on a JDBC 3.0 driver.
	 * Some JTA providers might support nested transactions as well.
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
	 */
	int PROPAGATION_NESTED = 6;
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐