亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Hibernate 會話沒有被正確終止

Java Hibernate 會話沒有被正確終止

慕尼黑5688855 2023-05-10 13:54:30
我正在為我的 Java 應用程序使用 Hibernate。當 hibernate 打開連接時,JDBC 瘦客戶端打開這樣的會話:完成 Oracle DB 的工作后,我關閉打開的會話。會話關閉并處于非活動狀態,而不是完全被殺死。我也嘗試在關閉會話之前使用session.flush()命令,但它再次不起作用。我應該怎么做才能完全殺死和刪除這些會話?一個數據庫連接的例子,最后關閉它:Session session = null;Transaction transaction = null;try{    session = DatabaseConnection.GetDatabaseSession();    transaction = session.getTransaction();    transaction.begin();    /*        Something something    */    transaction.commit();}catch (Exception exp){    exp.printStackTrace();}finally{    if (session != null && session.isOpen())    {        session.close();        session.flush();    }}我的GetDatabaseSession()方法:public static Session GetDatabaseSession() throws FileNotFoundException, NullModelException{    String connectionUrl = "jdbc:oracle:thin:@" + IPADDRESS + ":" + DBPORT + ":" + DATABASE;    Properties properties = new Properties();    properties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");    properties.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");    properties.put("hibernate.connection.url", connectionUrl);    properties.put("hibernate.connection.username", USERNAME);    properties.put("hibernate.connection.password", PASSWORD);    properties.put("hibernate.default_schema", SCHEME);    properties.put("hbm2ddl.auto", "update");    properties.put("hibernate.query.substitutions", "true 0, false 1");    return new Configuration()            .addProperties(properties)            .addAnnotatedClass(SOMECLASS.class)            .buildSessionFactory(                    new StandardServiceRegistryBuilder()                            .applySettings(properties)                            .build()).openSession();}
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

看起來我必須關閉SessionFactory實體才能完全終止由我的 Hibernate 打開的會話。代碼將是這樣的:


public static SessionFactory sessionFactory = null;


public static void KillSessionFactory()

{

? ? if (sessionFactory != null && sessionFactory.isOpen())

? ? {

? ? ? ? sessionFactory.close();

? ? }

}

我的問題的一種間接解決方案是?Signleton Hibernate SessionFactory


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號