java中的每个sql语句都会创建数据库连接

时间:2015-02-05 09:56:37

标签: jdbc

我在实用程序连接中提供了以下信息,它会让我知道连接的创建位置。

 StackTraceElement[] st = Thread.currentThread().getStackTrace();
 System.out.println(  "create connection called from " + st[2] );

当我运行我的类时,有3个select(Preparedstatements)sql语句,我看到正在创建3个连接。

请让我知道这是正常的行为,还是我做错了什么?

这是我的实用程序类

public class DBConnection {

    public static Connection getDBConnection() {
        String sURL="jdbc:mysql://localhost:3306/XXX";
        String sUserName="root";
        String sPwd="";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(sURL, sUserName,sPwd);

                 StackTraceElement[] st = Thread.currentThread().getStackTrace();
                System.out.println(  "create connection called from " + st[2] );

            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public static void close(Connection con)
    {
        if (con != null)
        {
            try
            {
                con.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement stmt, ResultSet rs) 
    {
        if (rs != null)
        {
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        if (stmt != null)
        {
            try
            {
                stmt.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
}

这是我的java文件

public class Services {

    public String getData(@QueryParam("test") String test)
    {
        Connection dbConnection = null;
        PreparedStatement preparedStmt1 = null;
        ResultSet ResultSet1 = null;
        PreparedStatement custIdPst = null;
        ResultSet ResultSet2 = null;
        PreparedStatement activeRestPst = null;
        ResultSet activeRestResultSet = null;
        try
        {
            dbConnection = DBConnection.getDBConnection();
            String sql = "select xxxx from xxx";
            preparedStmt1 = dbConnection.prepareStatement(sql);
            ResultSet1 = preparedStmt1.executeQuery();
            while(ResultSet1.next())
            {
            // do something
            }
            String sqlcustid = "select xxx from xxx? limit 1;";
            while(ResultSet2.next())
            {

            }
            for(String str : list)
            {
                try
                {
                    String sqlactiveresta = "select xxx from xxx ?";
                    activeRestResultSet = activeRestPst.executeQuery();
                    while(activeRestResultSet.next())
                    {
                    }

                }
                catch(Exception e )
                {
                    e.printStackTrace();
                }

            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBConnection.close(preparedStmt1,ResultSet1);
            DBConnection.close(custIdPst,ResultSet2);
            DBConnection.close(activeRestPst,activeRestResultSet);
            DBConnection.close(dbConnection);
        }

    }

}

我问的原因是因为在一些要求后我得到了以下内容 在我的服务器控制台

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
        at sun.reflect.GeneratedConstructorAccessor42.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)

请告诉我这种正常行为或我的代码是否有任何问题?

1 个答案:

答案 0 :(得分:2)

每次创建DriverManager.getConnection(sURL,sUserName,sPwd)时都会创建一个连接。

通常我们需要一个连接池系统。我们打开让5个连接,然后我们使用可用的连接。如果您使用Hibernate或Ibatis,他们会为您创建和管理连接池。 希望这有帮助。