Hibernate问题:“连接太多”

时间:2011-05-13 14:12:37

标签: java hibernate

我收到以下错误:

.
.
.
.
6844 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1040, SQLState: 08004
6844 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Data source rejected establishment of connection,  message from server: "Too many connections"
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
    at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
    at $Proxy0.beginTransaction(Unknown Source)
    at com.mycomp.myproj.matcher.Matcher.findMatch(Matcher.java:228)
    at com.mycomp.myproj.Confidence.Confidence.main(Confidence.java:160)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1105)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2186)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.GeneratedConstructorAccessor16.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    ... 11 more

我的程序的休眠位是这样的:

for loop_1        // this will execute say 2000 times 
{
    for loop_2       // this will execute say 1000 times
    {
        // Opening 2 sessions for 2 different databases

        Configuration config_1 = new Configuration().configure("Hibernate_1.cfg.xml");
        SessionFactory sessionFactory_1 = config_1.buildSessionFactory();
        Session session_1 = sessionFactory_1.getCurrentSession();
        session_1.beginTransaction();

        Configuration config_2 = new Configuration().configure("Hibernate_2.cfg.xml");
        SessionFactory sessionFactory_2 = config_2.buildSessionFactory();
        Session session_2 = sessionFactory_2.getCurrentSession();
        session_2.beginTransaction();

        doInsertDb_1(some_object_1, session_1);

        doUpdateDb_2(some_object_2, session_2);
    }
}

    public int doInsertDb_1(Object obj, Session session) {

        try {

            session.save(obj);
            session.flush();
            session.getTransaction().commit();

            return 1;

        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public int doUpdate_2(Object obj, Session session) {

        try {

            Query query = session.createQuery("" <Creating some query> );
            query.executeUpdate();
            session.getTransaction().commit();

            return 1;

        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

很明显,某个地方我没有正确关闭连接。我无法弄清楚在哪里?有人可以指导我吗?

非常感谢。

2 个答案:

答案 0 :(得分:4)

您是否确定每次循环都需要创建两个会话?在两个循环之前创建它们一次可能会解决您的问题并加速您的代码。但是,如果你坚持在内循环中打开它们,请务必事后close(),因为这至少应该释放你正在打开的数据库连接(但永远不会关闭。)

编辑:您应该暂停更改,直到完成所有更改,这样您就不会过早关闭连接。

编辑2 :由于您需要每次更新的结果来处理进一步的更新,您至少可以在循环外部移动创建SessionFactories。

答案 1 :(得分:2)

尝试在循环外创建会话