IDEMPIERE-455 Discover and fix FindBugs problems / Pattern: ODR_OPEN_DATABASE_RESOURCE

This commit is contained in:
Carlos Ruiz 2012-10-30 17:10:00 -05:00
parent 4eb7477046
commit 4eed3a8640
3 changed files with 40 additions and 6 deletions

View File

@ -127,6 +127,7 @@ public class MSequence extends X_AD_Sequence
} }
Connection conn = null; Connection conn = null;
Statement timeoutStatement = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -145,7 +146,7 @@ public class MSequence extends X_AD_Sequence
//postgresql use special syntax instead of the setQueryTimeout method //postgresql use special syntax instead of the setQueryTimeout method
if (DB.isPostgreSQL()) if (DB.isPostgreSQL())
{ {
Statement timeoutStatement = conn.createStatement(); timeoutStatement = conn.createStatement();
timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( QUERY_TIME_OUT * 1000 )); timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( QUERY_TIME_OUT * 1000 ));
} }
else if (DB.getDatabase().isQueryTimeoutSupported()) else if (DB.getDatabase().isQueryTimeoutSupported())
@ -264,6 +265,12 @@ public class MSequence extends X_AD_Sequence
DB.close(rs, pstmt); DB.close(rs, pstmt);
pstmt = null; pstmt = null;
rs = null; rs = null;
if (timeoutStatement != null){
try {
timeoutStatement.close();
}catch(Exception e){}
timeoutStatement = null;
}
if (conn != null) if (conn != null)
{ {
try { try {
@ -378,6 +385,7 @@ public class MSequence extends X_AD_Sequence
int docOrg_ID = 0; int docOrg_ID = 0;
int next = -1; int next = -1;
Statement timeoutStatement = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null; ResultSet rs = null;
try try
@ -430,7 +438,7 @@ public class MSequence extends X_AD_Sequence
//postgresql use special syntax instead of the setQueryTimeout method //postgresql use special syntax instead of the setQueryTimeout method
if (DB.isPostgreSQL()) if (DB.isPostgreSQL())
{ {
Statement timeoutStatement = conn.createStatement(); timeoutStatement = conn.createStatement();
timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( QUERY_TIME_OUT * 1000 )); timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( QUERY_TIME_OUT * 1000 ));
} }
else if (DB.getDatabase().isQueryTimeoutSupported()) else if (DB.getDatabase().isQueryTimeoutSupported())
@ -511,6 +519,10 @@ public class MSequence extends X_AD_Sequence
// Finish // Finish
try try
{ {
if (timeoutStatement != null) {
timeoutStatement.close();
timeoutStatement = null;
}
if (trx == null && conn != null) { if (trx == null && conn != null) {
conn.close(); conn.close();
conn = null; conn = null;

View File

@ -1101,6 +1101,7 @@ public final class DB
{ {
if (DB.isPostgreSQL()) if (DB.isPostgreSQL())
{ {
Statement timeoutStatement = null;
try try
{ {
Connection conn = cs.getConnection(); Connection conn = cs.getConnection();
@ -1119,16 +1120,26 @@ public final class DB
} }
finally finally
{ {
if (rs != null)
rs.getStatement().close();
DB.close(rs); DB.close(rs);
} }
} }
Statement timeoutStatement = conn.createStatement(); timeoutStatement = conn.createStatement();
timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( timeOut * 1000 )); timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( timeOut * 1000 ));
if (log.isLoggable(Level.FINEST)) if (log.isLoggable(Level.FINEST))
{ {
log.finest("Set statement timeout to " + timeOut); log.finest("Set statement timeout to " + timeOut);
} }
} catch (SQLException e) {} } catch (SQLException e) {}
finally{
if (timeoutStatement != null) {
try {
timeoutStatement.close();
} catch (Exception e) {}
timeoutStatement = null;
}
}
} }
else else
{ {
@ -1150,7 +1161,8 @@ public final class DB
{ {
if (DB.isPostgreSQL() && timeOut > 0) if (DB.isPostgreSQL() && timeOut > 0)
{ {
try { Statement timeoutStatement = null;
try {
if (autoCommit) if (autoCommit)
{ {
cs.getConnection().setAutoCommit(true); cs.getConnection().setAutoCommit(true);
@ -1159,7 +1171,7 @@ public final class DB
{ {
if (currentTimeout > 0) if (currentTimeout > 0)
{ {
Statement timeoutStatement = cs.getConnection().createStatement(); timeoutStatement = cs.getConnection().createStatement();
timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( currentTimeout * 1000 )); timeoutStatement.execute("SET LOCAL statement_timeout TO " + ( currentTimeout * 1000 ));
if (log.isLoggable(Level.FINEST)) if (log.isLoggable(Level.FINEST))
{ {
@ -1168,7 +1180,7 @@ public final class DB
} }
else else
{ {
Statement timeoutStatement = cs.getConnection().createStatement(); timeoutStatement = cs.getConnection().createStatement();
timeoutStatement.execute("SET LOCAL statement_timeout TO Default"); timeoutStatement.execute("SET LOCAL statement_timeout TO Default");
if (log.isLoggable(Level.FINEST)) if (log.isLoggable(Level.FINEST))
{ {
@ -1179,6 +1191,14 @@ public final class DB
} }
} catch (SQLException e) { } catch (SQLException e) {
} }
finally{
if (timeoutStatement != null) {
try {
timeoutStatement.close();
} catch (Exception e) {}
timeoutStatement = null;
}
}
} }
DB.close(cs); DB.close(cs);
} }

View File

@ -963,6 +963,8 @@ public class DB_PostgreSQL implements AdempiereDatabase
} }
finally finally
{ {
if (rs != null)
rs.getStatement().close();
DB.close(rs); DB.close(rs);
} }