-
Spring事务中奇怪的transactions2次的问题30
Spring与Hibernate集成
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="net.duohuo"></context:component-scan> <aop:aspectj-autoproxy /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="file:src/hibernate.cfg.xml"/> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/> </bean> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> </beans>
DAO层测试代码如下
package net.duohuo.hibernate.dao.impl; import javax.annotation.Resource; import net.duohuo.hibernate.bean.User; import net.duohuo.hibernate.dao.UserDao; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Repository @Transactional public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ @Resource(name="sessionFactory") public void setBaseDaoSessionFactory(SessionFactory baseDaoSessionFactory) { super.setSessionFactory(baseDaoSessionFactory); } @Override public void add() { this.getHibernateTemplate().save(new User("xxx")); } }
写了一个测试类
package net.duohuo.hibernate.test; import net.duohuo.hibernate.bean.User; import net.duohuo.hibernate.dao.UserDao; import net.duohuo.hibernate.util.HibernateSessionFactory; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.stat.Statistics; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class testSession { public void runwithoutSpring(){ SessionFactory sf=HibernateSessionFactory.getSessionFactory(); Session session=sf.openSession(); session.beginTransaction(); session.save(new User("xxx")); session.getTransaction().commit(); session.close(); Statistics stat=sf.getStatistics(); System.out.println(stat); } public void runwithSpring(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao dao=(UserDao)ctx.getBean("userDaoImpl"); dao.add(); SessionFactory fac=(SessionFactory)ctx.getBean("sessionFactory"); Statistics stat=fac.getStatistics(); System.out.println(stat); } public static void main(String args[]) { testSession test=new testSession(); test.runwithoutSpring(); test.runwithSpring(); } }
输出结果
Hibernate: insert into test.user (name) values (?)
Statistics[start time=1299479596150,sessions opened=1,sessions closed=1,transactions=1,successful transactions=1,optimistic lock failures=0,flushes=1,connections obtained=1,statements prepared=1,statements closed=1,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=1,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]
Hibernate: insert into test.user (name) values (?)
Statistics[start time=1299479597185,sessions opened=1,sessions closed=1,transactions=2,successful transactions=1,optimistic lock failures=0,flushes=1,connections obtained=1,statements prepared=1,statements closed=1,second level cache puts=0,second level cache hits=0,second level cache misses=0,entities loaded=0,entities updated=0,entities inserted=1,entities deleted=0,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,queries executed to database=0,query cache puts=0,query cache hits=0,query cache misses=0,max query time=0]这里用Spring进行事务管理时有两次transactions,换用XML配置事务时也是一样,求解!
问题补充:<div class="quote_title">zzzlyr 写道</div><div class="quote_div"> getCurrentSession() 用这个试一试,别用openSession()这个。 <br /> <br />你的事物已经让Spring 自动管理了。为什么还有在程序里边起动事物,在关呢。 <br /> <br />你在DAO层上整个类都加上事物的注解了@Transactional,应该不需要自己手动开启事物和关闭事物了吧! <br /></div> <br />runwithoutSpring()方法是用原始的Hibernate获取Session开启事务,保存,提交的。而runwithSpring()方法直接调用Dao中的方法的。
问题补充:还有在取消Dao层事务注解的时候事务没有提交,但Statistics打印出来,transactions=1,successful transactions=0。一旦加了注解就变成了transactions=2,successful transactions=1不晓得为什么
问题补充:<div class="quote_title">dwangel 写道</div><div class="quote_div">因为你在DAO类声明了@Transaction, <br />你看下HibernateTemplate的源代码就可以发现 <br />HibernateTemplate的save方法,最终会新建一个Session,新开一个事务,直到提交。 <br />那么事务建立就有两次: <br />Dao调用的proxy,HibernateTemplate.save</div> <br />可是问题是我将Dao类中的方法中的内容save都注释了,什么都不执行。他还是会打印出 <br />sessions opened=1,sessions closed=1,transactions=2,successful transactions=1,难道是@Transactional注解与继承了HibernateDaoSupport的原因?
问题补充:<div class="quote_title">eclipse_vic 写道</div><div class="quote_div">@Transactional <br />和后面的 begin ,启动了两次了!</div> <br />各位大哥,带begin的那个方法是我单独写出来对比的呀。。。
问题补充:研究了下,在配置Spring声明事务时,Spring在方法运行前检查当前线程是否绑定了session,而如果没有绑定则open一个放入SessionFactoryUtils的ThreadLocal中,这个绑定到当前线程的session的是需要使用SessionFactoryUtils获取的。通过查看HibernateDaoSupport的源码发现它集成了SessionFactoryUtils,HibernateDaoSupport的getSession源码如下 <br /><pre name="code" class="java">
# protected final Session getSession(boolean paramBoolean)
# throws DataAccessResourceFailureException, IllegalStateException
# {
# return ((!(paramBoolean)) ? SessionFactoryUtils.getSession(getSessionFactory(), false) : SessionFactoryUtils.getSession(getSessionFactory(), this.hibernateTemplate.getEntityInterceptor(), this.hibernateTemplate.getJdbcExceptionTranslator()));
# }
</pre> <br />相当于获取了这个绑定当前线程的currentSession,如果没有则由SessionFactoryUtils创建一个放入ThreadLocal。 <br />我把@Transactional去掉,注意现在完全是手动控制事务哦!dao中的代码如下 <br /><pre name="code" class="java">
public void create(Object basebean) {
Session session=this.getSession();
Transaction tx=session.beginTransaction();
tx.commit();
//this.releaseSession(session);
}
</pre> <br />这里故意没有关session <br />Status打印出sessions opened=1,sessions closed=0,transactions=1,successful transactions=1 <br />把上面的注释去掉 <br />Status打印出sessions opened=1,sessions closed=1,transactions=2,successful transactions=1 <br />奇怪的transactions=2出现了。。。。。 <br /><pre name="code" class="java">
# protected final void releaseSession(Session paramSession)
# {
# SessionFactoryUtils.releaseSession(paramSession, getSessionFactory());
# }
</pre> <br />看来基本上是Spring在关闭session时自动又commit了一次造成的2011年3月07日 14:42
9个答案 按时间排序 按投票排序
-
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
你配置的就是hibernate事物管理。。。。2011年3月09日 00:46
-
因为你在DAO类声明了@Transaction,
你看下HibernateTemplate的源代码就可以发现
HibernateTemplate的save方法,最终会新建一个Session,新开一个事务,直到提交。
那么事务建立就有两次:
Dao调用的proxy,HibernateTemplate.save2011年3月08日 11:45
-
你写的测试方法,一个是spring 自动管理事物,一个是相当于编程式事物(程序控制)!
1: public void runwithoutSpring()这个方法,无论你加不注解,都会事物提交,因为是你自己手动控制事物,跟Spring就无关了。
2: public void runwithSpring()方法,才是声明式事物,不用自己管理,只要配好相应的配置,spring 会自动管理,如果uncheck Exception 它会回滚掉
test.runwithoutSpring();
test.runwithSpring();
这好像是两个方法一起测试吧,你一个一个测试试一试。此回落只供参考,你试一下吧,不见的我说的对
2011年3月08日 10:24
-
http://today.java.net/pub/a/today/2006/08/31/jotm-transactions-in-spring-and-hibernate.html
参考这个...2011年3月07日 17:14
-
getCurrentSession() 用这个试一试,别用openSession()这个。
你的事物已经让Spring 自动管理了。为什么还有在程序里边起动事物,在关呢。
你在DAO层上整个类都加上事物的注解了@Transactional,应该不需要自己手动开启事物和关闭事物了吧!2011年3月07日 16:34
相关推荐
Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...
sql2o-spring-transactions-demo 使用 sql2o fork 设置 Spring 事务示例: :
Distributed transactions in Spring, with and without XA
Oracle自治事务的介绍(Autonomous_Transactions)PRAGMA_AUTONOMOUS_TRANSACTION
* Spring web: Spring MVC, Spring Web Flow 2, Spring Roo, other dynamic scripting, integration with popular Grails Framework (and Groovy), REST/web services, and more. This book guides you step by ...
Distributed Transactions in MySQL 分阶段提交事务
BEA系统宣布了他们在一个名为Pitchfork的项目上与Interface21的合作,该项目提供了基于Spring的Java EE 5编程模型的实现,包括支持用于注入(injection)、拦截( interception)和事务处理(transactions)的JSR-...
Chapter 2: Spring Basics Chapter 3: Basic Application Setup Chapter 4: Persistence with Hibernate Chapter 5: Domain Model Fundamentals Chapter 6: Transaction Management Chapter 7: Effective Testing ...
Other recipes include Spring enterprise solutions for topics such as Spring Java EE integration, Spring Integration, Spring Batch, Spring Remoting, messaging, transactions, and working with big data ...
Exam topics covered include tasks and scheduling, remoting, the Spring Web Services framework, RESTful services with Spring MVC, the Spring JMS module, JMS and JTA transactions with Spring, batch ...
#sqlite3-transactions 向添加事务支持。背景Node-sqlite3 是一... Sqlite3-transactions 通过在事务中透明地锁定数据库来解决这个问题。 如果数据库被锁定,所有不属于该事务的查询都会在事务完成后排队并执行。 作为
spring4 开发jar包 Introduction The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A ...
BEA系统宣布了他们在一个名为Pitchfork的项目上与Interface21的合作,该项目提供了基于Spring的Java EE 5编程模型的实现,包括支持用于注入(injection)、拦截( interception)和事务处理(transactions)的JSR-...
JAVA分布式事务atomikos atomikos jar 包 transactions-jdbc-4.0.4.jar
IEEE Transactions Latex Template
Covering version 5 of the Spring Framework, this book is the most comprehensive Spring reference and practical guide available for harnessing the power of this leading enterprise Java application ...
Spring Professional认证旨在测试和验证学生对 Spring 和 Spring Boot核心方面的理解和熟悉程度,例如:配置、组件扫描、AOP、数据访问和事务、REST、安全、自动配置、执行器、 Spring boot测试等。 认证对象: - ...
sails-mysql-transactions, 带有mySQL事务支持的帆/水线 ORM 带有复制支持的MySQL事务性 ORMsails-mysql-transaction 是用于MySQL的船帆ORM适配器,具有事务和复制群集支持。这个适配器在流行的sails-mysql 适配器...
Programming with Transactions Testing Commits and Rollbacks Almost all commercial-strength DBMS software supports the ability to undo the effects of a transaction before those effects are committed ...
37. Distributed Transactions with JTA 37.1. Using an Atomikos Transaction Manager 37.2. Using a Bitronix Transaction Manager 37.3. Using a Narayana Transaction Manager 37.4. Using a Java EE Managed ...