mysql - 如何解决 Hibernate 中连接过多的错误

标签 mysql struts2 hibernate3

我在我的 Web 应用程序中使用 struts2+hibernate3。它工作正常。有时显示无法打开连接。以下连接语句用于在 Action 类 中与休眠连接。

protected SessionFactory getSessionFactory() {
    try {  

        Configuration cfg = new Configuration(); 
        cfg.configure("hibernate.cfg.xml"); 
        SessionFactory factory = cfg.buildSessionFactory(); 
        return factory; 

    } catch (Exception e) {
        log.error("sessionFactory", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory");
    }
}
    public List viewAllPromotion() {
        System.out.println("finding student instance");
        try {
            Session session = sessionFactory.openSession();
            System.out.println("View All Student"); 
            session.beginTransaction(); 
            List results = session.createQuery("from   Student").list();  
            System.out.println("List got Rsults:"+results.size());

            session.close();  
            return results; 


        } catch (RuntimeException re) {  
            log.error("find by example failed", re);
            throw re;  
        }
    }

休眠配置文件:

 <session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/marksheet</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.search.autoregister_listeners">false</property>  
    <property name="hibernate.session_factory_name">MySessionFactory</property> 
    <property name="current_session_context_class">thread</property> 
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="hibernate.connection.pool_size">10</property>

    <property name="show_sql">true</property>
    <mapping resource="com/rewardz/model/student.hbm.xml" />   
    <mapping resource="com/rewardz/model/course.hbm.xml" />              
    <mapping resource="com/rewardz/model/subject.hbm.xml" />  
    <mapping resource="com/rewardz/model/staff.hbm.xml" />  
    <mapping resource="com/rewardz/model/Role.hbm.xml" />  
    <mapping resource="com/rewardz/model/Privilege.hbm.xml" />  
    <mapping resource="com/rewardz/model/Logtab.hbm.xml" />  
</session-factory>

当我进行更多交易时,我收到以下错误消息。

   HTTP Status 500 - type Exception report

   message

        descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

   exception

        org.hibernate.exception.JDBCConnectionException: Cannot open connection

注意 GlassFish Server 开源版 3.1.1 日志中提供了异常的完整堆栈跟踪及其根本原因。

谁能帮我解决这个问题?提前致谢。

最佳答案

您应该发布堆栈跟踪。我猜它会告诉您更多有关错误的信息。

此外,尝试使用像 c3p0 或 bonecp 这样的连接池。当不使用这些库之一时,您可能会遇到奇怪的连接问题,例如连接在打开时间过长后超时等。

关于mysql - 如何解决 Hibernate 中连接过多的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9223437/

相关文章:

javascript - struts2 jquery 表仅显示在第一个选项卡上

java - 在 JSP 中显示 BufferedReader

java - Struts 2 不从客户端填充 POJO

mysql - 有没有hibernate方言支持Mysql UTF-8国家字符类型?

java - 如何将 ResultSet 插入到新的 MYSQL 表中?

php - Laravel:通过 xp_start 和 xp_end 查找行

mysql - 更改 VARCHAR 数字

mysql - 如何远程连接MySQL数据库服务器?

java - 高性能 hibernate 插入

java - 非英文字符显示成这样?