Note that there are some explanatory texts on larger screens.

plurals
  1. POJava JDBC efficiency: How long should a connection be maintained?
    primarykey
    data
    text
    <p>I'm still working on the same problem mention <a href="https://stackoverflow.com/questions/11153322/java-swing-postgres-user-authentication-close-old-connection-when-new-connect">here</a>. It seems to work fine especially after creating an AbstractModel class shown below:</p> <pre><code>public abstract class AbstractModel { protected static Connection myConnection = SingletonConnection.instance().establishConnection(); protected static Statement stmt; protected static ResultSet rs; protected boolean loginCheck; // if userId and userLoginHistoryId are valid - true, else false protected boolean userLoggedIn; // if user is already logged in - true, else false public AbstractModel (int userId, Long userLoginHistoryId){ createConnection(); // establish connection loginCheck = false; userLoggedIn = false; if (userId == 0 &amp;&amp; userLoginHistoryId == 0){ // special case for login loginCheck = true; // 0, 0, false, false userLoggedIn = false; // set loginCheck to true, userLogged in to false } else { userLoggedIn = true; try{ String query = "select \"user_login_session_check\"(" + userId + ", " + userLoginHistoryId + ");"; System.out.println("query: " + query); stmt = myConnection.createStatement(); rs = stmt.executeQuery(query); while (rs.next()){ loginCheck = rs.getBoolean(1); } } catch (SQLException e){ System.out.println("SQL Exception: "); e.printStackTrace(); } } } // close connection public void closeConnection(){ try{ myConnection.close(); } catch (SQLException e){ System.out.println("SQL Exception: "); e.printStackTrace(); } } // establish connection public void createConnection(){ myConnection = SingletonConnection.instance().establishConnection(); } // login session check public boolean expiredLoginCheck (){ if (loginCheck == false &amp;&amp; userLoggedIn == true){ closeConnection(); return false; } else { return true; } } } </code></pre> <p>I've already posted the stored procedures and Singleton Pattern implementation in the link to the earlier question above.</p> <p>I'm under the impression that I don't need to close the connection to the database after each single data transaction, as it would just slow the application. I'm looking at about 30 users for this system I'm building, so performance and usability is important.</p> <p>Is it correct to prolong the connection for at least 3-4 data transactions? Eg. Validation checks to user inputs for some form, or, something similar to google's auto-suggest ... These are all separate stored function calls based on user input. Can I use one connection instance, instead of connecting and disconnecting after each data transaction? Which is more efficient?</p> <p>If my assumptions are correct (more efficient to use one connection instance) then opening and closing of the connection should be handled in the controller, which is why I created the createConnection() and closeConnection() methods.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload