我在我的应用程序中使用 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;
}
}
我的休眠配置文件是,
<?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)
任何建议都受到高度赞赏。
干杯!!