gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:24:24 24 4
gpt4 key购买 nike

我在我的应用程序中使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com