1
我在我的應用程序中使用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)
任何建議高度讚賞。
乾杯!
感謝您的回覆。現在我經常收到以下錯誤(在問題中編輯)。你能否建議我如何避免這種情況? –
感謝您的信息。我無法明白你的觀點。你能否詳細說明或引導我通過一個例子? –
謝謝你的好答案。我試着在我的服務器中,我再次收到以下錯誤,SEVERE:Servlet.service()爲servlet [jsp]在上下文中與path []拋出異常[org.hibernate.service.UnknownServiceException:未知服務請求[org。 hibernate.stat.spi.StatisticsImplementor]]根本原因 org.hibernate.service.UnknownServiceException:未知服務請求[org.hibernate.stat.spi.StatisticsImplementor] –