1

I am using struts2+hibernate3 in my web application.It is working fine. Some times shows Cannot open connection. Follwoing connection statement for connect with hibernate in Action class.

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;  
        }
    }

Hibernate configuration file:

 <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>

I am getting following error message when i do more transactions.

   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

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs.

can anyone help me to resolve this issue? Thanks in Advance.

shiva
  • 177
  • 2
  • 9
  • 24
  • you have started transcation in your code but never closed it? – Umesh Awasthi Feb 10 '12 at 06:20
  • @ Umesh Awasthi: I have used session.close() method. May i know how to close the transaction? – shiva Feb 14 '12 at 07:39
  • something like `tx.commit();` – Umesh Awasthi Feb 14 '12 at 07:43
  • @UmeshAwasthi: One more doubt on that. Can u tell me about the Session.close() method, Whether it is close connection from MySQl? – shiva Feb 14 '12 at 08:35
  • it End the session by releasing the JDBC connection and cleaning up.More more details refer to the docs.http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#connection%28%29 – Umesh Awasthi Feb 14 '12 at 08:45
  • @UmeshAwasthi: Thanks a lot for your Help. I missed to close the trasaction in all my methods. I used following code session.getTrasaction.commit(); . My Problem got Solved. Thank you so much Mr. UmeshAwasthi. – shiva Feb 15 '12 at 08:24

1 Answers1

0

You should post the stack trace. I'm guessing it tells you more about what's wrong.

Also, try using a connection pool like c3p0 or bonecp. You can run into weird connection problems when not using one of these libraries such as connections timing out after being open for too long, etc.

Ben McCann
  • 18,548
  • 25
  • 83
  • 101
  • I am not able to clear about c3p0 or bonecp. Can u explain about it.? – shiva Feb 14 '12 at 07:39
  • A connection pool library will hold open a number of connections and give you an existing connection when you need a connection that way you do not need to open a new one, which has performance benefits. They do other nice things like making sure the connection is open before giving it to you, which you will not get with Hibernate alone. I'm not sure whether it will solve your problem or not, but it is a nice thing to do regardless. https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool – Ben McCann Feb 14 '12 at 16:33
  • I missed to close my trasaction in all methods so it showed me Too Many Connection Error. Sure to read about boneCp/c3pO. Thanks a lot for your wonderful effort in my issue. – shiva Feb 15 '12 at 08:25