java 中的每个 sql 语句是否创建一个数据库连接

Does each sql statement in java creates a Database connection

我在我的实用程序连接中放置了以下内容,这将让我知道连接是从哪里创建的。

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

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

请告诉我这是正常现象,还是我做错了什么??

这是我的工具class

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)

如果这是正常行为或者我的代码有什么问题,请告诉我??

是的。

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

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