2016-05-19 14 views
0

タイトルでは、my ComboPooledDataSourceは各要求に対して新しいDB接続を作成し、接続を再利用または解放しないことを示しています。あらかじめPreparedStatements、ResultSets、Connectionでclose()を呼び出しています。私はTomcatとAmazon AWS Elastic BeanstalkとRDSを使用しています。関連するコードは以下の通りです:ComboPooledDataSourceは接続を再利用しません

Databaseクラス:DBコール

@GET 
@Path("/{session}") 
@Produces("application/json") 
public Response getAll(@PathParam("session") String session){ 

    JSONArray response = new JSONArray(); 
    Connection conn = null; 
    PreparedStatement prepared = null; 
    ResultSet rs = null; 

    try { 
     conn = DB.db.getDatabase(); 
     prepared = conn.prepareStatement("SQL STATEMENT"); 
     prepared.setString(1, session); 
     prepared.setTimestamp(2, new Timestamp(Time.current())); 

     rs = prepared.executeQuery(); 

     while(rs.next()){ 
      JSONObject obj = new JSONObject(); 
      obj.put("id", rs.getInt("id")); 
      obj.put("name", rs.getString("name")); 
      obj.put("color", rs.getString("color")); 

      response.put(obj); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
     return Response.status(500).build(); 
    } finally{ 
     try{ 
      prepared.close(); 
      rs.close(); 
      conn.close(); 
     } 
     catch (SQLException e){ 
      e.printStackTrace(); 
     } 
    } 
    return Response.status(200).entity(response.toString()).build(); 
} 

私が研究されていると機能するソリューションを見つけていないと

public class DB { 

ComboPooledDataSource dataSource; 

public static DB db = new DB(); 

private DB(){ 
    ComboPooledDataSource dataSource = new ComboPooledDataSource(); 
    try { 
     dataSource.setDriverClass("com.mysql.jdbc.Driver"); 

     String db = System.getProperty("RDS_DB_NAME"); 
     String username = System.getProperty("RDS_USERNAME"); 
     String password = System.getProperty("RDS_PASSWORD"); 
     String hostname = System.getProperty("RDS_HOSTNAME"); 
     String port = System.getProperty("RDS_PORT"); 

     String jdbcURL = "jdbc:mysql://" + hostname + ":" + port + "/" + db + "?user=" + username + "&password=" + password; 

     dataSource.setJdbcUrl(jdbcURL); 

     this.dataSource = dataSource; 
    } catch (PropertyVetoException e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* Get database name 
* @return Databse name 
*/ 
public static String dbN(){ 
    return DATABASE; 
} 

/** 
* Gets the database connection 
* @return Database connection 
* @throws SQLException 
*/ 
public Connection getDatabase() throws SQLException{ 
    return dataSource.getConnection(); 
}} 

API。私はconn.close()と他のすべての関数を呼び出すことは間違いないが、実際に閉じているものはないことを知っている。私はすべてのアイデアから誰も知りません

答えて

0

私はComboPooledDataSourceを捨てて、自分の基本プールと接続ラッパーを作成しました。接続ラッパーはConnectionクラスをラップし、close()およびisClosed()関数をブール値に置き換えます。プールは接続の検索時にそれらの多くを保持し、クローズされたプールを探しました。開いているプールが多すぎると閉じました。

関連する問題