0
我的小程序正常終止,如果我不開始交易保存到一個事務。休眠程序仍在運行
public static void main(String[] args){
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
SessionFactory sessionFactory = null;
Session session = null;
Transaction t = null;
try{
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
session.save(new UserInfo("[email protected]", "password2", "erical garb"));
t = session.getTransaction();
t.commit();
}
catch (Exception e){
System.out.println("Could not create connection!");
e.printStackTrace();
}
finally{
DAOUtilities.closeResource(session, "Session");
DAOUtilities.closeResource(sessionFactory, "sessionFactory");
StandardServiceRegistryBuilder.destroy(registry);
System.out.println("Session open: " + session.isOpen());
System.out.println("Factory open: " + sessionFactory.isOpen());
System.out.println("Transaction active: " + t.isActive());
}
}
打印語句確認session和sessionFactory已成功關閉,並且事務不再處於活動狀態。據我所知,沒有辦法檢查註冊表是否被成功銷燬。下面是輸出...
Hibernate: insert into UserInfo (loggedIn, name, password, email) values (?, ?, ?, ?)
Mar 24, 2017 12:37:49 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/hibernatedb]
Session open: false
Factory open: false
Transaction active: false
我缺少什麼?任何建議將appriciated,謝謝。
注意:我的數據庫gui正在檢測每次執行的一次會話泄漏,並且更改準確地反映在數據庫中。
您正在使用哪個版本的hibernate? –
休眠5.2.9。它是最新的「穩定」版本。我會嘗試使用舊版本,但我懷疑這是問題所在。 –