I'd like to share a database connection among servlets.
I created the connection pool and I obtain the dataSource object like this
Context envContext = (Context)context.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/limedb");
Now, I'd like to share this dataSource object among servlet so that each servlet can just do
connection = dataSource.getConnection();
to get its own connection.
What is the best method to achieve this? I'd like to create the pool at the application startup and store it somewhere...
There are actually a few ways you can do this.
It all depends on the use case, how familiar you are with java, the overall design of your application, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.