Antoine
Antoine

Reputation: 77

How to handle the Connection Object in my DAOs (Java EE)

I have to create a web app that connects to a MySQL DB. I was wondering how I should handle the connection object in my DAOs.

The application is likely to be used a few times a day by different users.

I am a bit troubled with the DAO pattern and using a static singleton. As it is a web app, I would have a connection to the DB permanently. I feel that it s not so good to keep this connection up and shared between all users sessions.

On the other hand, I'm not so sure that getting the connection and closing it every time I need to access the Db is really a better idea.

Is one way better than the other? did I forget anything that could be better?

Upvotes: 0

Views: 133

Answers (1)

Adrian Shum
Adrian Shum

Reputation: 40056

Normally you will have datasource setup in your container, and for each operation, just get connection from the datasource. With correct transaction manager setup, connections retrieved under same transaction should be the same. Connection pooling is normally handled in the datasource.

Upvotes: 1

Related Questions