Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>is my use in static Connection object thread safe?</em></p> </blockquote> <h1>Absolutely not!</h1> <p>This way the connection going to be shared among all requests sent by all users and thus all queries will interfere with each other. But threadsafety is not your only problem, resource leaking is also your other problem. You're keeping a single connection open during the entire application's lifetime. The average database will reclaim the connection whenever it's been open for too long which is usually between 30 minutes and 8 hours, depending on DB's configuration. So if your web application runs longer than that, the connection is lost and you won't be able to execute queries anymore.</p> <p>This problem also applies when those resources are held as a non-<code>static</code> instance variable of a class instance which is reused multiple times.</p> <p>You should <em>always</em> acquire and close the connection, statement and resultset in the <strong>shortest possible scope</strong>, preferably inside the very same <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html" rel="noreferrer"><code>try-with-resources</code> block</a> as where you're executing the query according the following JDBC idiom:</p> <pre class="lang-java prettyprint-override"><code>public User find(String username, String password) throws SQLException { User user = null; try ( Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT id, username, email FROM user WHERE username=? AND password=md5(?)"); ) { statement.setString(1, username); statement.setString(2, password); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { user = new User(); user.setId(resultSet.getLong("id")); user.setUsername(resultSet.getString("username")); user.setEmail(resultSet.getString("email")); } } } return user; } </code></pre> <p>Note that you should <strong>not</strong> return a <code>ResultSet</code> here. If you're not on Java 7 yet, then use a <code>try-finally</code> block wherein you manually close the closeable resources in the reverse order as you've acquired them.</p> <p>If you worry about connecting performance, then you should be using connection pooling instead. This is built-in into many Java EE application servers and even barebones servletcontainers like Tomcat supports it. Just create a JNDI datasource in the server itself and let your webapp grab it as <code>DataSource</code>. It's transparently already a connection pool.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/2299469/how-should-i-connect-to-a-mysql-data-source-from-eclipse/">How should I connect to JDBC database / datasource in a servlet based application?</a></li> <li><a href="https://stackoverflow.com/questions/8345133/when-my-app-loses-connection-how-should-i-try-to-recover">When my app loses connection, how should I recover it?</a></li> <li><a href="https://stackoverflow.com/questions/7592056/am-i-using-jdbc-connection-pooling">Am I Using JDBC Connection Pooling?</a></li> <li><a href="https://stackoverflow.com/questions/5003142/jsp-using-mvc-and-jdbc">Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern</a></li> <li><a href="http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html" rel="noreferrer">DAO tutorial with JDBC</a></li> </ul>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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