Cheers man, ive set up connection pooling to get a new connection each time, seems to work ok. I dunno I quite like the raw no framework approach so far
🙂 At least ive made more progress on the rest of this app without a framework than I did with a framework.
Iim a bit confused with regards to not reusing connections and the PreparedStatement class though. This is an initial method I wrote to get the users email and password from the database, I was using a Statement object but ive shoehorned in PreparedStatement because its its faster and protects from SQL injection:
Code:
private Map<String,String> getUserDetails(String loginAttemptEmail)
{
Map output = new HashMap<String,String>();
ResultSet resultSet = null;
String retrievedEmail = null;
String retrievedPassword = null;
PreparedStatement getEmailStatement = null;
String getUserEmailString = "SELECT email,password FROM usertable WHERE email = ?";
Connection databaseConnection = DatabaseUtils.getDatabaseConnection();
try
{
getEmailStatement = databaseConnection.prepareStatement(getUserEmailString);
getEmailStatement.setString(1,loginAttemptEmail);
resultSet = getEmailStatement.executeQuery();
resultSet.next();
retrievedEmail = resultSet.getString("email");
retrievedPassword = resultSet.getString("password");
}
catch (SQLException ex)
{
Logger.getLogger(AuthenticationServlet.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
DatabaseUtils.closeConnections(databaseConnection, resultSet, getEmailStatement);
}
if(retrievedEmail != null && retrievedPassword != null)
{
output.put(retrievedEmail, retrievedPassword);
return output;
}
else
{
return null;
}
}
It works but if I understand this all correctly then PreparedStatement is being kinda wasted here because its being recreated each method call with a different connection. Dosent this negate the idea of having a PreparedStatement? Is this a case where reusing the same connection would be a good plan?
EDIT: Aha here we go!
http://stackoverflow.com/questions/11889401/preparedstatement-pool-with-connection-pool
http://stackoverflow.com/questions/...ement-preparedstatement-and-resultset-in-jdbc
Seems that even though im closing the resources they're not actually gone they're just returned to the pool. And the database server stores the PreparedStatement in cache or something, so it should be fine.