Session/EntityManager is closed
Hinbernate操作数据库必须要开启事务, 但是在添加事务的时候遇到这个问题也是郁闷,
说Session被关闭了, 而这个Session又是必须的.
关键是我并没有关闭, 也找不到是哪里被关闭了的.

我把代码改成如下的样子, 则是可以运行的, 在执行之前,开启事务
/**
* 查询用户
* @param uid
* @return
*/
@Override
public User get(Integer uid) {
Transaction transaction = session.beginTransaction();
User user = null;
try {
user = session.get(User.class,uid);
transaction.commit();
} catch(Exception e) {
e.printStackTrace();
transaction.rollback();
}
return user;
}
原因分析: 每次调用了session之后, 在事务提交了以后就会把session关闭
这是因为Hibernate会维护这个Session, 在我提交事务的时候关闭Session
解决思路: 在代码执行之前, 开启Session, 添加如下代码:
@Override
public User get(Integer uid) {
Session session = HibernateUtil.getSession();
return session.get(User.class,uid);
}
问题解决!
附HibernateUtil的代码:
package com.bj186.crm.factory; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; /**
* Hinbernate工具类
*/
public class HibernateUtil { private static SessionFactory sessionFactory; private HibernateUtil() { } static {
//1. 声明配置对象,加载配置文件
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
//2. 将配置文件告诉session工厂
sessionFactory = configuration.buildSessionFactory();
} //获取session工厂
public static Session getSession() {
if(sessionFactory !=null ) {
return sessionFactory.getCurrentSession();
}
return null;
} }
Session/EntityManager is closed的更多相关文章
- hibernate报错org.hibernate.SessionException: Session was already closed
org.hibernate.SessionException: Session was already closedat org.hibernate.internal.SessionImpl.clos ...
- hibernate的异常 Session was already closed
今天写hibernate时候遇到一些异常 代码: Session session = sessionFactory.getCurrentSession(); session.beginTransact ...
- oracle dblink造成远程数据库session过多
现场报网公司数据库连不上,先检查了下数据库processes=1500,session=2200.我认为非常大啊.这个数据库没有几个人用. 查看v$session中的session最多是哪个machi ...
- FortiGate日志中session clash
1.出现于:FortiGate v5.0和v5.2 2.出现原因 Session clash messages appear in the logs when a new session is cre ...
- Spring framework3.2整合hibernate4.1报错:No Session found for current thread
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransact ...
- JPA(四):EntityManager
Persistence Persistence类使用于获取EntityManagerFactory实例,该类包含一个名为createEntityManagerFactory的静态方法. // 创建En ...
- Mina Session
Chapter 4 - Session The Session is at the heart of MINA : every time a client connects to the server ...
- How to configue session timeout in Hive
This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...
- JAVA PERSISTENCE API (JPA)
13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...
随机推荐
- Hello World 4 JQuery
Hello World 4 JQuery <html> <head> <script type="text/javascript" src=" ...
- Asp.net MVC 使用PagedList(新的已更名 为X.PagedList.Mvc) 分页
在asp.net mvc 中,可以bootstrap来作为界面,自己来写分页程序.也可以使用PagedList(作者已更名为 X.PagedList.Mvc)来分页. 1.首先,在NuGet程序包管理 ...
- 洛谷 - P1217 - 回文质数 - 枚举
https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...
- 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)
传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...
- P5167 xtq的神笔
传送门 题解 倍增也好二分也好,果然复杂度只要和\(\log\)插上关系就没我啥事了-- 首先由一个显而易见然而我完全没有发现的结论,设\(calc(l,r)\)表示区间\([l,r]\)的\(or\ ...
- 前端代码规范(转载 http://codeguide.bootcss.com/)
http://codeguide.bootcss.com/ HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 J ...
- tomcat复习
javaEE 服务器: java企业版服务器:是一个标准 BS/CS: 1> CS: client/Server 优点:易操作性比较高 缺点:客户端需要升级维护. 2> BS: Brows ...
- Five things that make Go fast-渣渣翻译-让GO语言更快的5个原因
原文地址:https://dave.cheney.net/2014/06/07/five-things-that-make-go-fast 翻译放在每个小段下面 Anthony Starks has ...
- Mantis优化改造(技术篇)
为什么要写这篇? 既然都过去这么久了,都回忆不起来了,为什么还要整理出来这篇文章呢? 这还要追溯到2018年3月份. 当时换工作,面试了国内某知名电视厂商. 简历上面写了我优化改造了bug管理系统ma ...
- 【Python】Python3.4+Matplotlib详细安装教程
网上找了很多教程,这个还不错. 传送门:https://blog.csdn.net/xqf1528399071/article/details/52233895