我正在为我的 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
添加回答
举报
0/150
提交
取消