Too many connections using hibernate and mysql

I am using Hibernate 3 and mysql sever 5.5 for myweb application with spring 3.0

I get an exception like too many connections ...

My java file where I create the session is as follows:

public class DBConnection {

    static{

    }

    public Session getSession(){

         Session session = null;
         SessionFactory sessionFactory= null;
         sessionFactory = new Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();
        return session;

    }

}

and I call this method when I need a session

as

Session session=new DBConnection().getSession();

and after

transaction.commit();

Close the session using

session.close();

please help me in solving the problem .......

my hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbname</property>
<property name="hibernate.connection.username">root</property>

  <property name="hibernate.connection.password">lax</property>
  <property name="hibernate.connection.pool_size">100</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <property name=""></property>

  <property name="hibernate.connection.release_mode">on_close</property>

</session-factory>
</hibernate-configuration>
+5
source share
4 answers

JAVA ISSUE

Please check this answer: fooobar.com/questions/1114610 / ...

MYSQL ISSUE

YOU NEED TO ENTER IN AS SUPERUS. THEN RUN THIS CODE:

SET GLOBAL MAX_CONNECTIONS = 200;

, MYSQL SERVER. , MYSQLD.

MAX_CONNECTIONS = 500

, , , .

, , MySQL Monitor, :

show processlist;

. , MySQL, :

 kill 2309344;
-2

, , , SessionFactory, . , - , . SessionFactory, , , , , .., .

, ( SessionFactory) .

public class DBConnection {

      private static SessionFactory factory;
      static {
            factory = new Configuration().configure().buildSessionFactory();
      }

      public Session getSession() {
            return factory.openSession();
      }

      public void doWork() {
           Session session = getSession();
           // do work.
           session.close();
      }

     // Call this during shutdown
     public static void close() {
          factory.close();
     }
}
+16

SessionFactory , Session, .

factory . , :

public class DBConnection {
    private static SessionFactory factory;
    static {
        factory = new Configuration().configure().buildSessionFactory();
    }

    public Session getSession() {
        return factory.openSession();
    }

    // Call this during shutdown
    public static void close() {
        factory.close();
    }
}

.

+2

Session session = SessionFactoryUtils.getSession(this.sessionFactory,
                true);

. DAO (@Transactional) , . . 5 . mysql. @Venu ,

show variables like "max_connections";

. ( ) 151. , "innodb_open_files" ​​ 300. , 300, ( , java).

, "max_connections" 300, , , , "innodb_open_files" , . , , . , , "max_connections".

mysql "/etc/mysql/my.cnf" max_connections

max_connections        = 1000

"innodb_open_files" , "300"

mysql> show variables like "innodb_open_files";

mysql> show processlist;

, . , ( ).

1000 , URL- -, , jmeter, () 1000. . 3000 , 1 . , , roseindia

. . , @Transaction (mysql , , ). - OpenSessionInViewFilter. OpenSessionInViewFilter ( OpenSessionInViewInterceptor) , , , mysql ( ). , , , mysql. .. 151 .

0

All Articles