当前位置:编程学习 > JAVA >>

服务器项目中Hibernate这样配置正确么,为什么我的jsp项目过段时间就出错呢

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/kong?autoReconnect=true
</property>
<property name="connection.username">rt</property>
<property name="connection.password">r</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">kfc</property>
 <!-- C3P0 -->  
        <property name="hibernate.c3p0.max_size">2000</property>  
        <property name="hibernate.c3p0.min_size">8</property>  
        <property name="hibernate.c3p0.timeout">25000</property>  
        <property name="hibernate.c3p0.max_statements">100</property>  
        <property name="hibernate.c3p0.idle_test_period">18000</property>  
        <property name="hibernate.c3p0.acquire_increment">2</property>  
        <property name="hibernate.c3p0.validate">true</property>  

        <property name="hibernate.c3p0.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>     
        <property name="hibernate.show_sql">false</property>  
<mapping resource="Bean/Messag.hbm.xml" />
<mapping resource="Bean/Message1.hbm.xml" />
<mapping resource="Bean/Messa.hbm.xml" />
<mapping resource="Bean/Messag33.hbm.xml" />
<mapping resource="Bean/Us.hbm.xml" />
<mapping resource="Bean/Tk.hbm.xml" />
<mapping resource="Bean/Meage2.hbm.xml" />
<mapping resource="Bean/Ssage4.hbm.xml" />
<mapping resource="Bean/Mes.hbm.xml" />
<mapping resource="Bean/Msse52.hbm.xml" />
<mapping resource="Bean/Essage53.hbm.xml" />
</session-factory> --------------------编程问答--------------------  <property name="hibernate.c3p0.timeout">25000</property>

这个给小了吧,还有连接用完后有没有释放,  需要看看你的java代码。 --------------------编程问答-------------------- 能解决这个问题万分感谢,帮忙看看吧
工具类
package Util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file. 
     * The default classpath location of the hibernate config file is 
     * in the default package. Use #setConfigFile() to update 
     * the location of the configuration file for the current session.   
     */
    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

static {
     try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
    }
    private HibernateSessionFactory() {
    }

/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

        return session;
    }

/**
     *  Rebuild hibernate session factory
     *
     */
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

/**
     *  return session factory
     *
     */
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
     *  return session factory
     *
     * session factory will be rebuilded in the next call
     */
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
     *  return hibernate configuration
     *
     */
public static Configuration getConfiguration() {
return configuration;
}

}
Transaction tx=getSession().beginTransaction();
getSession().delete(m);
tx.commit();
getSession().clear();
getSession().close();


DaoImpl中的处理过程,
--------------------编程问答-------------------- 最后那几行是DaoImpl中的处理过程, --------------------编程问答--------------------
引用 3 楼 qq1553143680 的回复:
最后那几行是DaoImpl中的处理过程,


hibernate本来就是有严格的jdbc操作工作流程,这些你尽量不要侵入,也就是说不要随便自己给关掉,或者打开,除非你有把握不会冲突才去这样操作... --------------------编程问答-------------------- 那我上面的 操作哪里有问题需要改呢,请指教 --------------------编程问答-------------------- 求大神啊
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,