错误的信息:
org.hibernate.SessionException: Session was already closed
at org.hibernate.internal.SessionImpl.close(SessionImpl.java:411)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.internal.ThreadLocalSessionContext
T
r
a
n
s
a
c
t
i
o
n
P
r
o
t
e
c
t
i
o
n
W
r
a
p
p
e
r
.
i
n
v
o
k
e
(
T
h
r
e
a
d
L
o
c
a
l
S
e
s
s
i
o
n
C
o
n
t
e
x
t
.
j
a
v
a
:
356
)
a
t
c
o
m
.
s
u
n
.
p
r
o
x
y
.
TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356) at com.sun.proxy.
TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)atcom.sun.proxy.Proxy25.close(Unknown Source)
at test.TestQuery.testAll(TestQuery.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
- 异常
注:其实这个异常并不影响正常的结果
,结果还是能够出来滴,只不过看的有点碍眼。。。
回归正题,先看整个工程结构
1.主要是在hibernate.cfg.xml里加入了这样的一个配置
<!-- session对象的生命周期与本地线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
2.然后在HibernateUtils工具类里配置了
return sessionFactory.getCurrentSession();// 从线程中获取session
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private final static Configuration cfg;
private final static SessionFactory sessionFactory;
static {
cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
}
private HibernateUtils() {
}
public static Session getSession() {
return sessionFactory.getCurrentSession();// 从线程中获取session
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
3.之后在调用的过程中,做一个查询全部的测试
package test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;
import bean.User;
import util.HibernateUtils;
/**
* Query查询测试<br/>
* create by LINKSINKE on 2020/3/25
*/
public class TestQuery {
@Test
public void testAll() {
SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from User");
List<User> lis = query.list();
for (User user : lis) {
System.out.println(user);
}
tx.commit();
session.close();
sessionFactory.close();
}
}
运行后的效果是能够查询出来,再回过头看看错误的信息,会发现session was already closed,这句意思就是session已经被关闭了
难道是我的那个测试里加入了session.close()??? 从而导致了这个错误???
去注释看看,再次运行后错误没有了。。。看来这个不需要再次手动关闭,线程那边帮我们处理了
翻了一下hibernate的参考文档