I have to make a getDBConnection method in my Java application. It gives a connection object, and so I have not stopped this connection in this method.
Now, I am implementing this method in my application in different ways at regular intervals, and closing them at the end of an attempt - the block at the end I thought that the connection after use Should be free. However, I have seen a large number of connections (about 50) in MySQL Administrator's Server Connection tab.
// Define a method to retrieve a database connection / PropDemo is a property class that retrieves database related values from a file, connection connection (getDBConnection) {// Institution of properties PropDemo prop = new propmeno (); Connection Con = Null; // parameters.properties is receiving values from the file string JdbcDriver = prop.getMessage ("JdbcDriver"); String jdbcUrlPrefix = prop.getMessage ("JdbcUrlPrefix"); String DBIP = Prop Metaset ("DBIP"); String DBName = prop.getMessage ("DBName"); String DBuser = Prop Metasez ("Dbusar"); String db passwords = prop metatets ("db passwords"); // loading and JDBC MySQL connector driver class Class.forName instantiating (JdbcDriver) .newInstance () try {; Con = DriverManager.getConnection (JdbcUrlPrefix + DBIP + "/" + DBName, DBUser, DBPassword); If (con.isClosed ()) Logger.log ("connection can not be established", "vm"); } Hold (exception e) {Logger.log ("exception:" + E, "VM"); Logger.log (Logger Stack2string (e), "vm"); } With return; }
I am also closing affiliate ResultSet and Statement Objects. What can be missing here?
I am planning to replace all the details with all the posts prepared for efficiency and security reasons. Will that help quite? What else can be done?
EDIT: This is just a core Java application that is repeatedly quarantining for some changes in a MySQL database through MySQL-JDBC connector. I am not using any framework like spring or hibernate
Your code looks smart
In this way you are making a new connection.
The error is probably that when you turn it off.
You should end it in the block at the end. / P>
Some additional questions
1) Are you certain that 50 programs will come from this program? It may be that some others are coming from their own office. To confirm this, you have to stop the program, and then look in your connection monitor.
2) Does your app use many connections simultaneously? Maybe when you are using 50 at the same time then this is a peak.
If you can post the code where you close the connection, there is a possibility that there is problem.
In addition to this, I would suggest using a connection pool. You can create your own or you can see the results from this page:
Comments
Post a Comment