【问题标题】:Java Hibernate Sessions Don't Get Killed CorrectlyJava Hibernate 会话没有被正确终止
【发布时间】:2019-08-01 07:50:27
【问题描述】:

我将 Hibernate 用于我的 Java 应用程序。

当休眠打开一个连接时,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();
}

【问题讨论】:

    标签: java oracle hibernate session


    【解决方案1】:

    看来我必须关闭 SessionFactory 实体才能完全终止我的 Hibernate 打开的会话。代码是这样的:

    public static SessionFactory sessionFactory = null;
    
    public static void KillSessionFactory()
    {
        if (sessionFactory != null && sessionFactory.isOpen())
        {
            sessionFactory.close();
        }
    }
    

    我的问题的一个间接解决方案是创建Signleton Hibernate SessionFactory,如下所述:Singleton Hibernate SessionFactory Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多