0
연결이 생성 된 곳에서 알 수 있도록 다음 유틸리티를 연결했습니다. java의 각 sql 문이 데이터베이스 연결을 생성합니까?
StackTraceElement[] st = Thread.currentThread().getStackTrace();
System.out.println( "create connection called from " + st[2]);
내가 3 개 연결이 생성되는 것을 볼 3 선택 (자는 PreparedStatements) SQL 문이있는에서 내 수업을 실행
.이 정상적인 동작인지 알려주세요. 아니면 잘못된 것이 있습니까?
이
내 유틸리티 클래스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();
}
}
}
}
입니다 그리고 이것은
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)
이 정상적인 동작 또는 이것이 가능한지 알려주십시오. 아무것도 내 코드에 잘못 ??
감사합니다. 각 preparedStmt1, ResultSet1을 닫는 것이 완료되면 곧바로 끝나야합니다. 그렇지 않으면 프로그램의 마지막에서 닫는 것이 좋습니다. – Kiran