Hibernate的session.connection错误:“用户太多”

时间:2011-02-09 01:23:41

标签: mysql hibernate jdbc

我正在使用Hibernate开发一个Java Web应用程序,但其中有一部分我想使用JDBC,因为我正在动态创建查找表。

使用我的网络应用程序几分钟后,我收到此错误:

  

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:   数据源拒绝建立   连接,来自服务器的消息:“太   许多关系“

我知道不推荐使用session.connection(),但我只想获得底层的JDBC连接。我尝试使用session.doWork(),但错误仍然像以前一样发生。

代码如下所示:

域层:

/**
 * Goes through the list of lookupTableAbstractions and persists each one
 * @param lookupData
 */
public void updateLookupValues( List<LookupTableAbstraction> lookupData )
{
    lookupTablesData.dropAllLookupTables(lookupData);

    lookupTablesData.createLookupTables(lookupData);

    for (LookupTableAbstraction lookupTable : lookupData)
        lookupTablesData.persistLookupTableValues(lookupTable);
}

数据层:

public LookupTableAbstraction getLookupTable( String tableName )
{
    LookupTableAbstraction lookupTable = new LookupTableAbstraction();

    Session session = getSessionFactory().openSession();

    String sqlQuery = "select value from " + tableName;

    List<String> lookupTableValues = session.createSQLQuery(sqlQuery).list();

    session.close(); 

    lookupTable.setTableName(tableName);
    for (String value : lookupTableValues)
        lookupTable.addValue(value);

    return lookupTable;
}

/**
 * Persists the passed in lookup table.
 * The lookup table that is used is determine by
 * the tableName field of the passed in lookupTable
 * @param lookupTable
 */
public void persistLookupTableValues( LookupTableAbstraction lookupTable )
{
    Session session = getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    Connection connection = null;
    try
    {
        connection = getJDBCConnectionFromHibernate(session);

        Statement stmt = connection.createStatement();

        String tableName = lookupTable.getTableName();
        for (String value : lookupTable.getValues() ) 
        {
            String sql = " insert into " + tableName + 
                         " (value) " +
                         " values " +
                         "('" + value + "')";

            System.out.println(sql);

            stmt.executeUpdate(sql);
        }
        stmt.close();
    }
    catch( Exception e )
    {
        System.out.println("Exception(persistLookupTableValues): " + e.getMessage());
        e.printStackTrace();
    }
    finally
    {

        try {
            tx.commit();
            connection.close();
            session.close();
        } catch (SQLException e) {
            System.out.println("Exception(persistLookupTableValues): " + e.getMessage());
            e.printStackTrace();
        }
    }
}

/**
 * Drop's all lookup tables.
 * It drops each table based off the lookupTableAbstractions in the passed in list
 * @param lookupData
 */
public void dropAllLookupTables( List<LookupTableAbstraction> lookupData )
{
    Session session = getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();

    Connection connection = null;
    try
    {
        connection = getJDBCConnectionFromHibernate(session);

        Statement stmt = null;
        for (LookupTableAbstraction lookupTableAbstraction : lookupData) {
            stmt = connection.createStatement();
            stmt.executeUpdate("drop table " + lookupTableAbstraction.getTableName());
        }

        stmt.close();
    }
    catch( Exception e )
    {
        System.out.println("Exception(dropAllLookupTables): " + e.getMessage());
        e.printStackTrace();
    }
    finally
    {
        try {
            tx.commit();
            connection.close();
            session.close();
        } catch (SQLException e) {
            System.out.println("Exception(dropAllLookupTables): " + e.getMessage());
            e.printStackTrace();
        }
    }
}

/**
 * Creates all lookup tables, one for each lookupTableAbstraction
 * in the passed in list
 * @param lookupData
 */
public void createLookupTables( List<LookupTableAbstraction> lookupData )
{
    Session session = getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();

    Connection connection = null;
    try
    {
        connection = getJDBCConnectionFromHibernate(session);

        Statement stmt = null;
        for (LookupTableAbstraction lookupTableAbstraction : lookupData) {
            stmt = connection.createStatement();
            stmt.executeUpdate("create table " + lookupTableAbstraction.getTableName() + 
                               " ( ID  int(11) auto_increment, " +
                               "  value text, " +
                               "  primary key (ID) )");
        }

        stmt.close();
    }
    catch( Exception e )
    {
        System.out.println("Exception(createLookupTables): " + e.getMessage());
        e.printStackTrace();
    }
    finally
    {
        try {
            tx.commit();
            connection.close();
            session.close();
        } catch (SQLException e) {
            System.out.println("Exception(createLookupTables): " + e.getMessage());
            e.printStackTrace();
        }
    }
}

protected Connection getJDBCConnectionFromHibernate( Session session )
{
    return session.connection();
}

感谢您的任何建议

2 个答案:

答案 0 :(得分:2)

这里的问题相同。互联网上的很多例子都忘了关闭会话工厂。如果你不关闭它,你将得到mysql“太多连接”错误。用这行代码关闭它:

fact.close();

假设你将它命名为 fact

SessionFactory fact = new Configuration().configure().buildSessionFactory();

答案 1 :(得分:1)

我解决了这个问题。

我一遍又一遍地创建会话工厂。

所以,我这样做了:

protected static SessionFactory sessionFactory = null;

static 
{
    sessionFactory = new Configuration().configure().buildSessionFactory();
}

/**
 * Returns a Hibernate session factory
 * @return
 */
protected static SessionFactory getSessionFactory()
{       
    return sessionFactory;
}