錯誤的信息:
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.ThreadLocalSessionContextProxy25.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的參考文檔