Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To many connection on MySql

Status
Not open for further replies.

HoMyGod

Programmer
Jul 17, 2000
59
CH
Hello,

I have a super class like this :

public ConnectTools()
{
try
{
this.connexion(Constant.hostName, Constant.userName, Constant.passWord, Constant.drvrName);
}
...

It allows me to connect to a MySql database.
I have several classes that inherit from ConnectTools. And a child class implement another class...so that, each time I implement one class, I have several connections on mySql.

My questions:

- How can I transform my code to have only one connection on mysql?
- How can I deconnect from the database when the user quit the browser (because the thread keep running while the session isn't finished)

Thank you for any help on this painful problem
H.

 
hello

1) make the connection property a 'static' property with the following declaration :

private static java.sql.Connection myConnection;

'myConnection' will be shared among all objects ... (because static)

2) redefine the 'finalize()' method that all classes inherit from the 'Object' class. this method is called when the object is to be destroyed, you can then disconnect from the database.

(if you use many object , perhaps you will need to use a counter to compute how many active objects are still alive, and countdown each time finalize() is called, and when reaching 0, disconnect from the database).

manu0
 
Ok, it works with one user. But if you have several users working at the same moment, you need more than one connection on the database (each user has a connection linked with the session opened by his browser). If you close a static connection, it close the connection for all the users at this moment...isn't it?
 
yes, you're rigth !

the solution of your problem is perhaps using aggregation instead of inheritance...

you can create an object 'ConnectTool' per session/user

and now instead of deriving your other classes from it, embed that 'ConnectTool' object in your others objects.

so you'll get one connection per user.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top