gpt4 book ai didi

java - 如何在Spring中使用Tomcat 7提供的JNDI DataSource?

转载 作者:行者123 更新时间:2023-11-29 00:00:10 26 4
gpt4 key购买 nike

我遵循了这个答案 How to use JNDI DataSource provided by Tomcat in Spring?但我遇到了以下异常:

java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:524)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:493)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:273)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:224)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:120)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:143)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:132)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

tomcat 服务器.xml :

<GlobalNamingResources>
<Resource
name="jdbc/welcome-kit"
global="jdbc/welcome-kit"
auth="Container"
type="javax.sql.DataSource"
username="name"
password="pass"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="host/port:test"
maxActive="8"
maxIdle="4"
/>

tomcat 上下文.xml

<ResourceLink global="jdbc/welcome-kit" name="jdbc/welcome-kit" type="javax.sql.DataSource" />

网络应用程序 web.xml:

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/welcome-kit</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

Spring 上下文.xml:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/welcome-kit">
</bean>

而且我还在 tomcat/lib 中添加了 ojdbc.jar

最佳答案

在 web.xml 中

<resource-ref>
<res-ref-name>myDatasource</res-ref-name>
<res-type>javax.sql.XADataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

在您的 spring 应用程序上下文中:

<!-- Define the JNDI datasource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/myDatasource" />
</bean>

关于java - 如何在Spring中使用Tomcat 7提供的JNDI DataSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043368/

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