[英]Share connection pool among servlets
I'd like to share a database connection among servlets. 我想在servlet之间共享数据库连接。
I created the connection pool and I obtain the dataSource object like this 我创建了连接池,并获得了像这样的dataSource对象
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 现在,我想在servlet之间共享此dataSource对象,以便每个servlet都可以
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. 这完全取决于用例,您对Java的熟悉程度,应用程序的整体设计等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.