This is a rookie question. What’s the best place to put
@Resource
private DataSource ds;
in a web application? Do I put it in a servlet, context listener or maybe there’s a better place for it?
Also, do I create a new Connection object in my doGet()/doPost() or should I do it somewhere else?
What’s the best practice for stuff like this?
Thank you!
In the very same class where you’d like to call
DataSource#getConnection().You usually do that in a method of a DAO class where you’d like to interact with the DB, in a
tryblock where you close theConnection(andStatementandResultSet, if any) in thefinallyblock.In a more abstracted and flexible setup, you could also do
DataSource#getConnection()in a DAO manager class or a transaction manager class.