Java Linux操作系统中Tomcat服务器的Hibernate连接问题
您好,我正在使用Linux操作系统和Fedora 17版,我的Hibernate配置文件如下Java Linux操作系统中Tomcat服务器的Hibernate连接问题,java,mysql,linux,hibernate,tomcat,Java,Mysql,Linux,Hibernate,Tomcat,您好,我正在使用Linux操作系统和Fedora 17版,我的Hibernate配置文件如下 <hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.con
<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:3306/Test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.connection.pool_size">40</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_periods">3000</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
</hibernate-configuration>
</session-factory>
我的连接代码如下:
public class DBConnection{
private static SessionFactory factory;
private static DBConnection singleton = new DBConnection();
// private static final SessionFactory sessionFactory;
private DBConnection(){ }
/* Static 'instance' method */
public static DBConnection getInstance( ) {
return singleton;
}
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();
}
// Call this during shutdown
public static void close() {
factory.close();
factory=null;
}
由于这个原因,我的应用程序速度变慢了,我可以在MySQL中看到睡眠查询
如何解决这个问题?如何在此环境中进行配置?非常感谢您的帮助
谢谢 会话对象在Hibernate中不是线程安全的。除非同步会话,否则是否跨多个线程使用同一会话objects@KeerthiRamanathan感谢您的回复-我在应用程序中跨多个线程使用同一会话。在这个环境中该怎么做?我可以问你为什么要这样做吗?在我的应用程序中,我对来自web应用程序的每个请求使用单独的会话创建,并且在函数本身的每个请求之后关闭它,session session=null;尝试{session=new DBConnection().getSession();tx=session.beginTransaction();tx.commit();session.close();DBConnection.close();}catch(异常示例){if(tx!=null)tx.rollback();session.close();DBConnection.close();ex.printStackTrace();}请查找上面的代码。如果你需要,我会给你额外的信息
public class DBConnection{
private static SessionFactory factory;
private static DBConnection singleton = new DBConnection();
// private static final SessionFactory sessionFactory;
private DBConnection(){ }
/* Static 'instance' method */
public static DBConnection getInstance( ) {
return singleton;
}
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();
}
// Call this during shutdown
public static void close() {
factory.close();
factory=null;
}