长时间在MySql中进行Sleep Connection导致Hibernate中的慢进程

时间:2013-10-28 05:11:10

标签: java mysql performance hibernate

我在我的应用程序中使用Hibernate,

我的连接代码如下,

public class DBConnection{

    private static SessionFactory factory; 
    private static ServiceRegistry serviceRegistry;

    public  DBConnection()
       {
        try
          { 
                      factory = new Configuration().configure().buildSessionFactory();

          }
          catch (Throwable ex) { 
                    System.err.println("Failed to create   sessionFactory object." + ex);
                    throw new ExceptionInInitializerError(ex); 
                }
       }

    public Session getSession() 
    {
          return factory.openSession();
    }


   // Call this during shutdown
  public static void close() {
        factory.close();
        serviceRegistry=null;
        factory=null;

   }
}

我的Hibernate配置文件是,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>


   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver

   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost:3307/Test
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      root
   </property>
     <property name=" hibernate.dbcp.max Wait">100</property>
</session-factory>


</hibernate-configuration>

此过程的示例代码是

 public String showurl(int id)
    {
        int i = 1;
        String url= "";
        Transaction tx= null;
        Session session = null;
        try
        {
            session = new DBConnection().getSession();
            tx = session.beginTransaction();
            Query query = session.createQuery(" FROM Test WHERE ID = :id");
            query.setInteger("id", id);

            List<Test> testlist= query.list();
            for(Test nl:testlist)
            {
                url= nl.geturl();
            }


        tx.commit();
        session.close();
        DBConnection.close();


    }catch(Exception ex)
    {
        if(tx!=null) tx.rollback();
        session.close();
        DBConnection.close();
        ex.printStackTrace();
    }
    finally
    {
        if(tx!=null)
        {
        tx=null;
        }
        if(session.isOpen())
        {
            session.close();
        }
        if(session!=null)
        {
            session= null;
        }
    }

        return url; 

    }

我的问题在于,由于数据库中的大量睡眠查询导致进程变慢。如何在我的应用程序中解决此问题。我有超过50个用户同时使用该应用程序。如何解决这个问题?

使用此功能时,我经常收到以下错误,

SEVERE: Servlet.service() for servlet [jsp] in context with path [] threw exception [org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor]] with root cause
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:126)
        at org.hibernate.internal.SessionFactoryImpl.getStatisticsImplementor(SessionFactoryImpl.java:1468)
        at org.hibernate.internal.SessionFactoryImpl.getStatistics(SessionFactoryImpl.java:1464)
        at org.hibernate.internal.SessionImpl.close(SessionImpl.java:346)

任何建议都受到高度赞赏。

干杯!!

1 个答案:

答案 0 :(得分:1)

我会从使用其他连接池管理器开始,而不是使用Hibernate的内置管理器 - 它不是为生产使用而设计的。尝试使用C3P0Here is little hint如何为hibernate配置它。可以找到更多信息here

修改 我刚刚注意到,每次你想操作od db时你都在创建新的会话工厂。那是错误!您应该只有 ONE 会话工厂实例,作为服务或应用程序范围的静态字段,以后用于根据需要创建会话。

 public static SessionFactory getSessionFactory(){
     if (factory == null) {

         try {
             factory = new Configuration().configure().buildSessionFactory();

         } catch (Throwable ex) {
             System.err.println("Failed to create   sessionFactory object." + ex);
             throw new ExceptionInInitializerError(ex);
         }
     }
     return factory;

 }

 public static Session getSession() {
     return getSessionFactory().openSession();
 }

直接从代码中使用getSession()