java - 长时间在 MySql 中 hibernate 连接导致 Hibernate 中的进程缓慢

标签 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; 

    }

我的问题是,由于数据库中的大量 sleep 查询导致进程变慢。如何在我的应用程序中解决这个问题。我有 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)

非常感谢任何建议。

干杯!

最佳答案

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

编辑: 我刚刚注意到,每次您想要操作 od db 时,您都在创建新的 session 工厂。那太错了!您应该只有 ONE session 工厂实例,作为服务或应用程序范围静态字段,然后根据需要使用它来创建 session 。

 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()

关于java - 长时间在 MySql 中 hibernate 连接导致 Hibernate 中的进程缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19627567/

相关文章:

MySQL Order By in Right Join 子句

java - 从为父类(super class)创建的链表中插入和提取子类

MySQL正在等待为什么我可以从命令行登录?

java - Eclipse E4 RCP 全局错误处理

php - Laravel使用ajax刷新数据库记录更新页面

objective-c - Swift 有标记指针吗?

java - 递归解析 JSON 对象 - Java

c# - 如何通过 BlToolkit 更新集合

java - 从 Restful Web 服务异步访问数据

java - 如何在RecyclerView中使用HashMap数据?