Java Web 应用程序有哪些不同的方式来存储 JDBC 连接字符串和参数?
Java Web 应用程序有哪些不同的方式来存储 JDBC 连接字符串和参数?
我知道的两种方法是: 1.应用端 2.环境方面(JNDI)
有人可以详细说明一下吗?就像在应用程序端使用属性文件一样。但是是否可以使用多个应用程序可以访问的外部属性文件?
仅供参考:我的环境包括 Tomcat
What kind of different ways are there for an Java web application to store the JDBC connection string and parameters?
The two ways of doing this that I am aware of is:
1. application side
2. environment side (JNDI)
Could someone elaborate on these? Like on the application side using a properties file. But is it possible to use an external properties file that multiple applications would have access to?
FYI: My environment includes Tomcat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
环境端的优点是它为所有 Web 应用程序提供单个即用型连接池,因此您不需要从每个单独的 Web 应用程序上对其进行配置/编程。应用程序端的优势在于您可以从 Web 应用程序对其进行 100% 的控制。然而,这种优势是值得商榷的。存在潜在的代码重复并且维护很笨拙。
是的,只需将其放在共享类路径位置即可(在 Tomcat 中,您可以通过
shared.loader 设置它
属性)。您只需要自己控制每个 Web 应用程序中的连接(池)创建。更推荐使用 JNDI 连接池数据源。/conf/catalina.properties
的The environment side has the advantage that it offers a single ready-to-use connection pool for all webapps, so that you don't need to configure/program it from every individual webapplication on. The application side has the advantage that you have 100% control over it from the webapplication on. This advantage is however discutable; there's potential code duplication and the maintenance is clumsy.
Yes, just put it in a shared classpath location (in Tomcat, you can set it by
shared.loader
property of/conf/catalina.properties
). You only need to control the connection (pool) creation in every webapplication yourself. Using a JNDI connection pooled datasource is more recommended.您可以从应用程序端使用连接池(如果您使用 Spring 框架,那么配置 Commons-DBCP 池非常容易)。
我建议在 Tomcat 的 server.xml 文件中定义数据源,并在您的 web 应用程序中添加一个 META-INF/context.xml 文件,其中的资源链接指向 Tomcat 数据源的 JNDI 位置。这样您就可以与 Tomcat 中部署的其他 Web 应用程序共享相同的连接池。
Yyou can use a connection pool from the application side (if you use Spring framework it is really easy to configure a Commons-DBCP pool).
I recommend to define the DataSource in Tomcat's server.xml file, and in your webapp add a META-INF/context.xml file with a Resource Link pointing to the JNDI location of the Tomcat's DataSource. This way you can share the same connection pooling with other webapps deployed in Tomcat.