So I wrote a method to just get a database table and it returns the resultset. But, it seems to be hanging on this method call. I THOUGHT it was returning just a pointer but I’m still new to programming. Is this call returning a pointer of Resultset or is it passing the whole thing. My main question is, why would this slow down the program? The table isn’t that big.
public ResultSet getEmployeeTable() throws SQLException
{
ResultSet rsEmpl;
Connection con = getDBConnection();
PreparedStatement pstmt;
String query;
query = "Select * from PTO_Employee where enabled = ?";
pstmt = con.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
pstmt.setInt(1, 1);
rsEmpl = pstmt.executeQuery();
return rsEmpl;
}
It’s connecting the DB which takes the most time. This can last in range of 100ms up to 1 second. That’s also why most webapps are using a so-called connection pool. A connection pool contains a bunch of already-opened connections which the webapp can then just reuse. You’ll only need to rewrite your JDBC code to adhere the JDBC idiom of opening-and-closing in a
try-finallyblock, otherwise you would later still have performance problems because you never return the connection to the pool and the pool would run out of them.Further, at the moment you’re returning the
ResultSet, it does not contain any records in the memory. This will usually only be retrieved from the DB and filled in Java’s memory when you callnext()for the first time. However, passing theResultSetaround outside the context where the SQL query is been invoked is a very bad practice. It should normally be mapped to a collection of entities.See also: