Commit d8a46836 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Database logic improvements.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@4776 b35dd754-fafc-0310-a699-88a17e54d16e
parent cdfdbee3
...@@ -59,11 +59,12 @@ public class DefaultUserProvider implements UserProvider { ...@@ -59,11 +59,12 @@ public class DefaultUserProvider implements UserProvider {
public User loadUser(String username) throws UserNotFoundException { public User loadUser(String username) throws UserNotFoundException {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_USER); pstmt = con.prepareStatement(LOAD_USER);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (!rs.next()) { if (!rs.next()) {
throw new UserNotFoundException(); throw new UserNotFoundException();
} }
...@@ -71,7 +72,6 @@ public class DefaultUserProvider implements UserProvider { ...@@ -71,7 +72,6 @@ public class DefaultUserProvider implements UserProvider {
String email = rs.getString(2); String email = rs.getString(2);
Date creationDate = new Date(Long.parseLong(rs.getString(3).trim())); Date creationDate = new Date(Long.parseLong(rs.getString(3).trim()));
Date modificationDate = new Date(Long.parseLong(rs.getString(4).trim())); Date modificationDate = new Date(Long.parseLong(rs.getString(4).trim()));
rs.close();
return new User(username, name, email, creationDate, modificationDate); return new User(username, name, email, creationDate, modificationDate);
} }
...@@ -79,10 +79,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -79,10 +79,7 @@ public class DefaultUserProvider implements UserProvider {
throw new UserNotFoundException(e); throw new UserNotFoundException(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -155,10 +152,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -155,10 +152,7 @@ public class DefaultUserProvider implements UserProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new User(username, name, email, now, now); return new User(username, name, email, now, now);
} }
...@@ -189,9 +183,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -189,9 +183,7 @@ public class DefaultUserProvider implements UserProvider {
abortTransaction = true; abortTransaction = true;
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeTransactionConnection(pstmt, con, abortTransaction);
catch (Exception e) { Log.error(e); }
DbConnectionManager.closeTransactionConnection(con, abortTransaction);
} }
} }
...@@ -199,23 +191,20 @@ public class DefaultUserProvider implements UserProvider { ...@@ -199,23 +191,20 @@ public class DefaultUserProvider implements UserProvider {
int count = 0; int count = 0;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_COUNT); pstmt = con.prepareStatement(USER_COUNT);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
count = rs.getInt(1); count = rs.getInt(1);
} }
rs.close();
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return count; return count;
} }
...@@ -229,26 +218,23 @@ public class DefaultUserProvider implements UserProvider { ...@@ -229,26 +218,23 @@ public class DefaultUserProvider implements UserProvider {
List<String> usernames = new ArrayList<String>(500); List<String> usernames = new ArrayList<String>(500);
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USERS); pstmt = con.prepareStatement(ALL_USERS);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
// Set the fetch size. This will prevent some JDBC drivers from trying // Set the fetch size. This will prevent some JDBC drivers from trying
// to load the entire result set into memory. // to load the entire result set into memory.
DbConnectionManager.setFetchSize(rs, 500); DbConnectionManager.setFetchSize(rs, 500);
while (rs.next()) { while (rs.next()) {
usernames.add(rs.getString(1)); usernames.add(rs.getString(1));
} }
rs.close();
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return usernames; return usernames;
} }
...@@ -257,10 +243,11 @@ public class DefaultUserProvider implements UserProvider { ...@@ -257,10 +243,11 @@ public class DefaultUserProvider implements UserProvider {
List<String> usernames = new ArrayList<String>(numResults); List<String> usernames = new ArrayList<String>(numResults);
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = DbConnectionManager.createScrollablePreparedStatement(con, ALL_USERS); pstmt = DbConnectionManager.createScrollablePreparedStatement(con, ALL_USERS);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
DbConnectionManager.setFetchSize(rs, startIndex + numResults); DbConnectionManager.setFetchSize(rs, startIndex + numResults);
DbConnectionManager.scrollResultSet(rs, startIndex); DbConnectionManager.scrollResultSet(rs, startIndex);
int count = 0; int count = 0;
...@@ -268,16 +255,12 @@ public class DefaultUserProvider implements UserProvider { ...@@ -268,16 +255,12 @@ public class DefaultUserProvider implements UserProvider {
usernames.add(rs.getString(1)); usernames.add(rs.getString(1));
count++; count++;
} }
rs.close();
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new UserCollection(usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
...@@ -300,10 +283,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -300,10 +283,7 @@ public class DefaultUserProvider implements UserProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -325,10 +305,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -325,10 +305,7 @@ public class DefaultUserProvider implements UserProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -350,10 +327,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -350,10 +327,7 @@ public class DefaultUserProvider implements UserProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -375,10 +349,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -375,10 +349,7 @@ public class DefaultUserProvider implements UserProvider {
throw new UserNotFoundException(sqle); throw new UserNotFoundException(sqle);
} }
finally { finally {
try { if (pstmt != null) pstmt.close(); } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -410,6 +381,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -410,6 +381,7 @@ public class DefaultUserProvider implements UserProvider {
List<String> usernames = new ArrayList<String>(50); List<String> usernames = new ArrayList<String>(50);
Connection con = null; Connection con = null;
Statement stmt = null; Statement stmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
stmt = con.createStatement(); stmt = con.createStatement();
...@@ -433,20 +405,16 @@ public class DefaultUserProvider implements UserProvider { ...@@ -433,20 +405,16 @@ public class DefaultUserProvider implements UserProvider {
} }
sql.append(" email LIKE '").append(StringUtils.escapeForSQL(query)).append("'"); sql.append(" email LIKE '").append(StringUtils.escapeForSQL(query)).append("'");
} }
ResultSet rs = stmt.executeQuery(sql.toString()); rs = stmt.executeQuery(sql.toString());
while (rs.next()) { while (rs.next()) {
usernames.add(rs.getString(1)); usernames.add(rs.getString(1));
} }
rs.close();
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (stmt != null) { stmt.close(); } } DbConnectionManager.closeConnection(rs, stmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new UserCollection(usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
...@@ -475,6 +443,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -475,6 +443,7 @@ public class DefaultUserProvider implements UserProvider {
List<String> usernames = new ArrayList<String>(50); List<String> usernames = new ArrayList<String>(50);
Connection con = null; Connection con = null;
Statement stmt = null; Statement stmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
stmt = con.createStatement(); stmt = con.createStatement();
...@@ -498,7 +467,7 @@ public class DefaultUserProvider implements UserProvider { ...@@ -498,7 +467,7 @@ public class DefaultUserProvider implements UserProvider {
} }
sql.append(" email LIKE '").append(StringUtils.escapeForSQL(query)).append("'"); sql.append(" email LIKE '").append(StringUtils.escapeForSQL(query)).append("'");
} }
ResultSet rs = stmt.executeQuery(sql.toString()); rs = stmt.executeQuery(sql.toString());
// Scroll to the start index. // Scroll to the start index.
DbConnectionManager.scrollResultSet(rs, startIndex); DbConnectionManager.scrollResultSet(rs, startIndex);
int count = 0; int count = 0;
...@@ -506,16 +475,12 @@ public class DefaultUserProvider implements UserProvider { ...@@ -506,16 +475,12 @@ public class DefaultUserProvider implements UserProvider {
usernames.add(rs.getString(1)); usernames.add(rs.getString(1));
count++; count++;
} }
rs.close();
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(e); Log.error(e);
} }
finally { finally {
try { if (stmt != null) { stmt.close(); } } DbConnectionManager.closeConnection(rs, stmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return new UserCollection(usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment