Answer the question
In order to leave comments, you need to log in
How to properly use the connections to the database received from tomcat'a?
Good afternoon!
I trusted tomcat to make connections to the database.
<resource-ref>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
InitialContext cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/TestDB");
Connection сonnection = ds.getConnection();
Answer the question
In order to leave comments, you need to log in
The DataSource is thread-safe, it can be obtained during initialization and stored in a field. But there is no Connection and it is better to use it within a single method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try (Connection con = ds.getConnection()) {
// применяете
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question