Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like Tomasz said, you should <strong>never</strong> <em><strong>ever</em></strong> depend on the fact that your application will be used by a small number of clients. The fact that the driver <strong><em>will</em></strong> timeout after a certain amount of time does not guarantee you that you will have enough available connections. Picture this: a lot of databases come pre-configured with a maximum number of connections set to (say) 15 and a timeout of (let's say) 10-15 minutes. If you have 30 clients and each does an operation, somewhere around half-way you'll be stuck short on connections.</p> <p>You should handle connections, files, streams and other resources the following way:</p> <pre><code>public void doSomething() { Connection connection = null; Statement stmt = null; ResultSet rs = null; final String sql = "SELECT ...."); try { connection = getConnection(); stmt = connection.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { // Do something here... } } catch (SQLException e) { e.printStackTrace(); } finally { closeResultSet(rs); closeStatement(stmt); closeConnection(connection); } } </code></pre> <p>The try/catch/finally guarantees you that the connection will get closed no matter the outcome. If there is some sort of failure, the finally block will still close the connection, just like it would do, if things were okay.</p> <p>Similarly, with file and streams you need to do the same thing. Initialize the respective object as <code>null</code> outside your try/catch/finally, then follow the approach above.</p> <p>This misconception makes a lot of Java applications misbehave under Windows, where people don't close files (streams to files, etc) and these files become locked, forcing you to either kill the JVM, or even restart your machine.</p> <p>You can also use a connection pool such as for example Apache's DBCP, but even then you should take care of closing your resources, despite the fact that internally, the different connection pool implementations do not necessarily close the connections.</p>
    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. 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