我有另一个奇怪的Hibernate问题。我用Google搜索并搜索了答案,但找不到我理解的任何内容。
当一个人导航到我们的主页时,它会打开1到59个连接/会话。它永远不会一致。他们通常不会关闭,但有时会关闭。在这方面也没有明显的一致性。我尝试使用getStatistics只返回零。
DAO.java
package com.grandcircus.spring.util;
import com.grandcircus.spring.controller.HomeController;
import com.grandcircus.spring.models.FamiliesEntity;
import com.grandcircus.spring.models.UsersEntity;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.stat.Statistics;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
/**
* Class description
*
* @author Sarah Guarino
* @version 1.0
*/
@Repository
@Transactional
public class DAO {
private static Configuration configurationObject = new Configuration().configure("hibernate.cfg.xml");
private static SessionFactory sessionFactory = configurationObject.buildSessionFactory();
public static FamiliesEntity newFamily(String famName) {
Session browsingSession = sessionFactory.openSession();
Transaction databaseTransaction = browsingSession.beginTransaction();
FamiliesEntity newFamily = new FamiliesEntity();
newFamily.setName(famName);
browsingSession.save(newFamily);
databaseTransaction.commit();
browsingSession.close();
return newFamily;
}
public static void newUser(String fName, String lName,
String email, String password,
int usergroup, int familyid) {
Session browsingSession = sessionFactory.openSession();
Transaction databaseTransaction = browsingSession.beginTransaction();
UsersEntity user = new UsersEntity();
user.setFname(fName);
user.setLname(lName);
user.setEmail(email);
user.setUsergroup(usergroup);
user.setPassword(password);
user.setFamilyid(familyid);
browsingSession.save(user);
databaseTransaction.commit();
browsingSession.close();
}
public static void updateUserCoordinates(String checkinLat,
String checkinLong,
String userId) {
Session browsingSession = sessionFactory.openSession();
Transaction myTransaction = browsingSession.beginTransaction();
Criteria criteria = browsingSession.createCriteria(UsersEntity.class);
UsersEntity personCheckingIn = (UsersEntity) criteria
.add(Restrictions.eq("userid", Integer.parseInt(userId)))
.uniqueResult();
personCheckingIn.setLastlat(checkinLat);
personCheckingIn.setLastlong(checkinLong);
personCheckingIn.setLasttime(HomeController.getCurrentTime());
browsingSession.save(personCheckingIn);
myTransaction.commit();
browsingSession.close();
}
public static boolean doesUserExist(String email) {
// this will pass if the email exists, or fail if the user does not exist.
boolean doesThisExist = true;
Session browsingSession = sessionFactory.openSession();
Criteria usersCriteria = browsingSession.createCriteria(UsersEntity.class);
try {
UsersEntity newUser = (UsersEntity) usersCriteria
.add(Restrictions.eq("email", email))
.uniqueResult();
if(newUser.getEmail() == null) {
doesThisExist = false;
}
} catch (Exception e) {
doesThisExist = false;
} finally {
browsingSession.close();
}
return doesThisExist;
}
public static boolean doesFamilyExist(int famId) {
boolean doesThisExist = true;
Session browsingSession = sessionFactory.openSession();
Criteria familyCriteria = browsingSession.createCriteria(FamiliesEntity.class);
try {
FamiliesEntity family = (FamiliesEntity) familyCriteria
.add(Restrictions.eq("familyid", famId))
.uniqueResult();
if(family.getFamilyid() == 0) {
doesThisExist = false;
}
} catch (NullPointerException e) {
doesThisExist = false;
} finally {
browsingSession.close();
}
return doesThisExist;
}
public static UsersEntity getUserByEmail(String email) {
Session browsingSession = sessionFactory.openSession();
Criteria userCriteria = browsingSession.createCriteria(UsersEntity.class);
UsersEntity user = (UsersEntity) userCriteria
.add(Restrictions.eq("email", email))
.uniqueResult();
browsingSession.close();
return user;
}
public static UsersEntity loadThisAccount(String userId) {
Session browsingSession = sessionFactory.openSession();
Criteria userCriteria = browsingSession.createCriteria(UsersEntity.class);
UsersEntity user = (UsersEntity) userCriteria
.add(Restrictions.eq("userid",
Integer.parseInt(userId)))
.uniqueResult();
browsingSession.close();
return user;
}
public static ArrayList<UsersEntity> loadChildAccounts(int familyId) {
Session browsingSession = sessionFactory.openSession();
Criteria childCriteria = browsingSession.createCriteria(UsersEntity.class);
ArrayList<UsersEntity> children = (ArrayList<UsersEntity>) childCriteria
.add(Restrictions.eq("familyid", familyId))
.add(Restrictions.eq("usergroup", 1))
.list();
browsingSession.close();
return children;
}
public static FamiliesEntity loadFamily(int familyId) {
Session browsingSession = sessionFactory.openSession();
Criteria familyCriteria = browsingSession.createCriteria(FamiliesEntity.class);
FamiliesEntity family = (FamiliesEntity) familyCriteria
.add(Restrictions.eq("familyid", familyId))
.uniqueResult();
browsingSession.close();
return family;
}
public static UsersEntity loadParentAccount(int familyId) {
Session browsingSession = sessionFactory.openSession();
Criteria adminCriteria = browsingSession.createCriteria(UsersEntity.class);
UsersEntity parent = (UsersEntity) adminCriteria
.add(Restrictions.eq("familyid", familyId))
.add(Restrictions.eq("usergroup", 0))
.uniqueResult();
browsingSession.close();
return parent;
}
}
的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/checkin</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<mapping class="com.grandcircus.spring.models.FamiliesEntity"/>
<mapping class="com.grandcircus.spring.models.LocationsEntity"/>
<mapping class="com.grandcircus.spring.models.UsersEntity"/>
<mapping resource="FamiliesEntity.hbm.xml"/>
<mapping resource="LocationsEntity.hbm.xml"/>
<mapping resource="UsersEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:2)
这里有几个问题。主要问题,我认为导致会话泄漏的原因是在doUserExist和didFamilyExist中,你经常得到NullPointerException而且从不关闭会话。其余问题在这里:
1)您的DAO使用Transactional注释,但所有方法都是静态的。删除注释或创建方法实例范围并将会话工厂注入DAO。最好的做法是依赖事务性注释,让像Spring或EJB3这样的容器来处理事务开始/提交调用。
2)您没有正确关闭会话,您需要将它们包装在try / finally块中并关闭finally块中的会话。最佳实践是依靠Spring或EJB3来处理会话管理。
3)永远不要捕获NullPointerException。始终检查对象是否为null并在那里添加适当的逻辑。
<强>更新强>
这是您的一些DAO方法的修改版本。这对您来说是最容易修复的,但我建议在适当的Spring,Hibernate和JPA集成的某些时候改进你的DAO:
public static FamiliesEntity newFamily(String famName) {
Session browsingSession = null;
Transaction databaseTransaction = null;
try{
browsingSession = sessionFactory.openSession();
databaseTransaction = browsingSession.beginTransaction();
FamiliesEntity newFamily = new FamiliesEntity();
newFamily.setName(famName);
browsingSession.save(newFamily);
databaseTransaction.commit();
return newFamily;
}catch(RuntimeException e){
if (databaseTransaction != null){
try{ databaseTransaction.rollback(); }
catch(RuntimeException ex){throw ex;}
}
throw e;
}finally{
if (browsingSession != null){
browsingSession.close();
}
}
}
public static boolean doesUserExist(String email) {
Session browsingSession = null;
try{
browsingSession = sessionFactory.openSession();
UsersEntity newUser = (UsersEntity) usersCriteria
.add(Restrictions.eq("email", email))
.uniqueResult();
if(newUser.getEmail() == null) {
return false;
}else{
return true;
}
}finally{
if (browsingSession != null){
browsingSession.close();
}
}
}
public static UsersEntity getUserByEmail(String email) {
Session browsingSession = null;
try{
browsingSession = sessionFactory.openSession();
Criteria userCriteria = browsingSession.createCriteria(UsersEntity.class);
UsersEntity user = (UsersEntity) userCriteria
.add(Restrictions.eq("email", email))
.uniqueResult();
}finally{
if (browsingSession != null){
browsingSession.close();
}
}
return user;
}
以下是改变了:
1)会话在try块之外定义,并在finally块中关闭。确保在关闭会话之前始终检查会话是否已实际初始化。否则,如果sessionFactory.openSession()抛出异常或返回null,则会在finally块中获得NPE。
2)事务,就像在try块之外定义会话一样,在try中初始化并在try块中提交。但是我们仍然需要处理这样的情况:如果在commit之前的任何代码抛出异常,我们就不会保持事务处于打开状态,这就是为什么我们需要将它回滚到catch块中。由于会话创建引发异常,再次事务在catch块中可以为null。
答案 1 :(得分:0)
建议使用连接池 您不必自己打开会话
答案 2 :(得分:0)
Tsolakp有很好的建议,它解决了我遇到的许多松散的连接泄漏问题。但是,这不是真正的答案。
如果您的项目在AWS上,您还应该完全确保包含您的数据库用户名和密码的WAR存档未被推送到Git或其他地方。这通常会导致您的连接遭到非常激进的攻击。</ p>