gpt4 book ai didi

具有数据源部署的 Tomcat 6 webapp

转载 作者:行者123 更新时间:2023-11-28 21:47:13 24 4
gpt4 key购买 nike

在开发环境和生产环境中部署具有数据源的应用程序的最佳方式是什么?

推荐使用 META-INF/context.xml 来指定 Tomcat 上下文,但我不明白如何在 context.xml 中指定数据源:

  1. 将数据库密码放在所有人都可以查看的context.xml中是不安全的;

  2. 如何为生产模式和开发模式维护两个不同的数据源?

你是如何解决这个问题的?

最佳答案

I don't understand how should I specify datasource in context.xml:

所以:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
<Resource
type="javax.sql.DataSource"
name="jdbc/dbname"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname"
username="java"
password="d$7hF_r!9Y"
maxActive="100" maxIdle="30" maxWait="10000"
/>
</Context>

并且在 web.xml :

<resource-env-ref>
<resource-env-ref-name>jdbc/dbname<resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

另见:


1: it's unsafe to put database password in context.xml which can be viewed by all;

网络用户无法查看。它只能由需要了解它们的服务器管理员查看。


2: how do I maintain two different datasources for production and devel mode?

定义两个独立的<Resource>每个都有不同的名称,并通过 web.xml 中的一些参数切换开发/生产模式或属性文件,以便您可以动态获取一个或另一个数据源。例如

<context-param>
<param-name>dev</param-name>
<param-value>true</param-value>
</context-param>

boolean dev = Boolean.valueOf(getServletContext().getInitParameter("dev"));

if (dev) {
dataSource = getDataSource("jdbc/devdb");
} else {
dataSource = getDataSource("jdbc/proddb");
}

关于具有数据源部署的 Tomcat 6 webapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4649173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com