Hibernate SessionFactory getCurrentSession is not valid without active transaction
2015-09-21 13:23
453 查看
I am facing one problem in Hibernate. Here is the code.
And in my cfg file
when i run the application with above code it is giving me an
I don’t know what behavior its performing internally. Any idea’s please.
if you look at the Java doc of
Obtains the current session. The definition of what exactly “current” means controlled by the
So your
However if you used
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction trans = session.beginTransaction(); trans.begin(); Session session2 = factory.getCurrentSession(); System.out.println(session2.isConnected()); trans.commit();
And in my cfg file
<session-factory> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433</property> <property name="hibernate.connection.username">username</property> <property name="connection.password">password</property> <property name="connection.pool_size">5</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">false</property> <mapping resource="Test.hbm.xml"/> </session-factory>
when i run the application with above code it is giving me an
Exceptionsaying “
org.hibernate.HibernateException: isConnected is not valid without active transaction”
I don’t know what behavior its performing internally. Any idea’s please.
if you look at the Java doc of
SessionFactory.html#getCurrentSession
Obtains the current session. The definition of what exactly “current” means controlled by the
CurrentSessionContext implconfigured for use.
So your
sessionand
session2are two different sessions. So you will have to start the transaction on
session2to access
isConnected().
However if you used
getCurrentSession()to retrieve first session then second time
getCurrentSession()will return you the same instance.
Session session = factory.getCurrentSession();//Use getCurrentSession rather than openSession Transaction trans = session.beginTransaction(); trans.begin(); Session session2 = factory.getCurrentSession();//Same session will be returned. System.out.println(session2.isConnected()); trans.commit();
相关文章推荐
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- 杰奇登录后的东西都是在session里面的
- ASP中SESSION无法保存问题的解决办法
- Oracle中的Connect/session和process的区别及关系介绍
- Node.js编程中客户端Session的使用详解
- 浅谈COOKIE和SESSION区别
- 解析PHP的session过期设置
- php中Session的生成机制、回收机制和存储机制探究
- php中将一个对象保存到Session中的方法
- 深入解析Session是否必须依赖Cookie
- PHP Session机制简介及用法
- 新手菜鸟必读:session与cookie的区别
- 深入讲解PHP Session及如何保持其不过期的方法
- PHP Session变量不能传送到下一页的解决方法
- PHP中的session永不过期的解决思路及实现方法分享
- *.ashx文件不能访问Session值的解决方法
- ASP.NET中Session和Cache的区别总结
- ASP.NET中在不同的子域中共享Session的具体方法
- Asp.net中判断一个session是否合法的方法