sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-11-29 00:24:24 hai mươi bốn 4
mua khóa gpt4 Nike

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

我的连接代码如下,

public class DBConnection{

private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;

public DBConnection()
{
thử
{
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 配置文件是,


"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">






org.hibernate.dialect.MySQLDialect


com.mysql.jdbc.Driver





jdbc:mysql://localhost:3307/Test


gốc rễ


gốc rễ

100




这个过程的示例代码是

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

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


tx. cam kết();
session.close();
DBConnection.close();


}catch(Exception ex)
{
if(tx!=null) tx.rollback();
session.close();
DBConnection.close();
ex.printStackTrace();
}
Cuối cùng
{
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)

非常感谢任何建议。

干杯!

1 Câu trả lời

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

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

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

thử {
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/

hai mươi bốn 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com