我注册了一个域 www.mywebapp.com 并将名称服务器指向我的 tomcat 主机。 所以当我给出 www.mywebapp.com 时,它会点击
tomcat/webapps/mywebapp.com
并且页面加载没有问题。所以大部分是正确的。
但是我无法为我的应用程序建立数据库连接(使用 JNDI 数据源),因为当我调用 -
DBConnection.ds = (DataSource)ctx.lookup("java:comp/env/jdbc/atfdb");
它说
DB Connection NamingException error-->Name atfdb is not bound in this Context.
注意事项:
我正在使用 tomcat7。
如果我将应用程序直接部署在tomcat/webapps 文件夹下,则会建立数据库连接,只有当我将其部署在webapps/mywebapp.com 时才会出现问题。
如果我们在 webapps/mywebapp.com 中部署 war 文件,JNDI 查找是否有任何特殊情况?
下面是我的配置
server.xml inside tomcat/conf/server.xml
-------------------------------------------
<Host appBase="webapps/mywebapp.com" name="mywebapp.com" unpackWARs="true" autoDeploy="true">
<Alias>www.mywebapp.com</Alias>
<Context path="" docBase="ROOT" debug="0" reloadable="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" prefix="mywebapp.com" suffix=".txt" timestamp="true"/>
</Host>
context.xml in which I am configuring my datasource.
context.xml inside webapps/mywebapp.com/META-INF
--------------------------------------------------
<Context>
<Resource name="jdbc/atfdb" auth="Container" type="javax.sql.DataSource"
removeAbandoned="true" maxActive="100" maxIdle="30" maxWait="10000"
removeAbandonedTimeout="30" username="spadmin" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/socialdb?autoReconnect=true"/>
</Context>
最佳答案
我会在您的 web.xml
中添加一个资源引用:
<resource-ref>
<description>atfdb description</description>
<res-ref-name>jdbc/atfdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
这是 Java EE 标准所期望的,因此当您使用其他文件夹时它可以工作的事实可能只是侥幸...
关于jakarta-ee - 当应用程序部署在 tomcat 的 webapps 中的文件夹内时数据源失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13511456/