Java Web 应用程序有哪些不同的方式来存储 JDBC 连接字符串和参数?

发布于 2024-12-17 17:13:05 字数 175 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-12-24 17:13:05

据我所知,执行此操作的两种方法是:1.应用程序端2.环境端(JNDI)有人可以详细说明这些吗?就像在应用程序端使用属性文件一样。

环境端的优点是它为所有 Web 应用程序提供单个即用型连接,因此您不需要从每个单独的 Web 应用程序上对其进行配置/编程。应用程序端的优势在于您可以从 Web 应用程序对其进行 100% 的控制。然而,这种优势是值得商榷的。存在潜在的代码重复并且维护很笨拙。


但是是否可以使用多个应用程序可以访问的外部属性文件?

是的,只需将其放在共享类路径位置即可(在 Tomcat 中,您可以通过 shared.loader 设置它/conf/catalina.properties 属性)。您只需要自己控制每个 Web 应用程序中的连接(池)创建。更推荐使用 JNDI 连接池数据源。

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.

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.


But is it possible to use an external properties file that multiple applications would have access to?

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.

初见终念 2024-12-24 17:13:05

您可以从应用程序端使用连接池(如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文