Note that there are some explanatory texts on larger screens.

plurals
  1. POJDBC Connection Issue
    primarykey
    data
    text
    <p>I have create a getDBConnection method in my Java application. This returns a connection object, and hence I haven't closed this connection in this method itself. </p> <p>Now, I am invoking this method from various methods in my application at regular intervals, and closing them inside a try - finally block. I thought this should free up the connection after use. However, I am seeing a large number of connections opened (about 50) in the MySQL Administrator's Server Connections tab. </p> <pre><code>//Defining a method to retrieve a database connection // PropDemo is a properties class that retrieves Database related values from a file public Connection getDBConnection() { //Instantiating the Properties object PropDemo prop = new PropDemo(); Connection con = null; // Retrieving values from the parameters.properties file String JdbcDriver = prop.getMessage("JdbcDriver"); String JdbcUrlPrefix = prop.getMessage("JdbcUrlPrefix"); String DBIP = prop.getMessage("DBIP"); String DBName = prop.getMessage("DBName"); String DBUser = prop.getMessage("DBUser"); String DBPassword = prop.getMessage("DBPassword"); try { // Loading and instantiating the JDBC MySQL connector driver class Class.forName(JdbcDriver).newInstance(); con = DriverManager.getConnection(JdbcUrlPrefix + DBIP + "/" + DBName, DBUser, DBPassword); if (con.isClosed()) Logger.log("Connection cannot be established", "vm"); } catch (Exception e) { Logger.log("Exception: " + e, "vm"); Logger.log(Logger.stack2string(e), "vm"); } return con; } </code></pre> <p>I am also closing the associated ResultSet and Statement Objects. What could be missing here? </p> <p>I am planning to replace all the Statements with PreparedStatements for efficiency and security reasons. Will that help significantly? What else can be done?</p> <p>EDIT: This is just a core java application that is repeatedly quering for changes in some fields in a MySQL database through MySQL-JDBC connector. I am not using any framework like Spring or Hibernate. </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.
 

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