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的更多相关文章

  1. hibernate报错org.hibernate.SessionException: Session was already closed

    org.hibernate.SessionException: Session was already closedat org.hibernate.internal.SessionImpl.clos ...

  2. hibernate的异常 Session was already closed

    今天写hibernate时候遇到一些异常 代码: Session session = sessionFactory.getCurrentSession(); session.beginTransact ...

  3. oracle dblink造成远程数据库session过多

    现场报网公司数据库连不上,先检查了下数据库processes=1500,session=2200.我认为非常大啊.这个数据库没有几个人用. 查看v$session中的session最多是哪个machi ...

  4. FortiGate日志中session clash

    1.出现于:FortiGate v5.0和v5.2 2.出现原因 Session clash messages appear in the logs when a new session is cre ...

  5. Spring framework3.2整合hibernate4.1报错:No Session found for current thread

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransact ...

  6. JPA(四):EntityManager

    Persistence Persistence类使用于获取EntityManagerFactory实例,该类包含一个名为createEntityManagerFactory的静态方法. // 创建En ...

  7. Mina Session

    Chapter 4 - Session The Session is at the heart of MINA : every time a client connects to the server ...

  8. How to configue session timeout in Hive

    This article explains how to configure the following settings in Hive:hive.server2.session.check.int ...

  9. JAVA PERSISTENCE API (JPA)

    13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java proje ...

随机推荐

  1. thiis also a test

    EL表达式 1.EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号, ...

  2. YARN(MapReduce 2)运行MapReduce的过程-源码分析

    这是我的分析,当然查阅书籍和网络.如有什么不对的,请各位批评指正.以下的类有的并不完全,只列出重要的方法. 如要转载,请注上作者以及出处. 一.源码阅读环境 需要安装jdk1.7.0版本及其以上版本, ...

  3. mtr和nmap命令

    mtr mtr是一个网络连通性判断工具,它可以结合ping nslookup tracert 来判断网络的相关特性. [root@10.10.90.97 ~]# mtr -h usage: mtr [ ...

  4. 洛谷 - P2762 - 太空飞行计划问题 - 最小割

    https://www.luogu.org/problemnew/solution/P2762 最小割对应的点,在最后一次更新中dinic的bfs会把他的dep重置掉.所以可以根据这个性质复原最小割. ...

  5. 878. Nth Magical Number

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  6. LuoguP2846[USACO08NOV]光开关Light Switching【线段树维护区间异或】By cellur925

    题目传送门 题目大意,给你一串灯,按一下开关可以将灯的状态取反(开变成关,关变成开).维护这个序列的两种操作:询问区间内有多少灯是开着的,区间按灯. 开始想的是分别维护区间内0的数量,1的数量,两个懒 ...

  7. Folding UVA - 1630

    题目 ans[i][j]表示由原串第i个字符到第j个字符组成的子串的最短折叠长度如果从i到j本身可以折叠,长度就是本身长度或折叠后的长度的最小值***此处参考:http://blog.csdn.net ...

  8. Lucas+中国剩余定理 HDOJ 5446 Unknown Treasure

    题目传送门 题意:很裸,就是求C (n, m) % (p1 * p2 * p3 * .... * pk) 分析:首先n,m<= 1e18, 要用到Lucas定理求大组合数取模,当然p[]的乘积& ...

  9. solr 查询获取数量getCount()

    //前期设置好查询条件和参数 long numFound = 0; SolrQuery query = new SolrQuery("*:*"); query.setQuery(& ...

  10. springboot之项目打包

    通过win中的cmd或者idea中终端,打包并启动项目: 1.mvn package     [打包,在target中生成jar] 2.java -jar xxxxx.jar  [启动jar]