欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在spring用jndi得到tomcat中的datasource的问题

程序员文章站 2022-05-21 14:00:12
...

今天想把项目中配置在spring中的数据源移到tomcat中来配置,遇见了一些问题,请各位指教,小弟感谢了。

目的:从spring中得到tomcat中配置的datasource。

我的做法:

在tomcat的context.xml文件中GlobalNamingResources标签中加入如下代码:

<Resource
          name="dataSource"
          type="javax.sql.DataSource"
          password="54mwr"
          driverClassName="org.postgresql.Driver"
          maxIdle="3"
          maxWait="5000"
          username="alemis"
          url="jdbc:postgresql://localhost:5432/alemis_dev"
          maxActive="10"/>

在tomcat的context.xml文件中Host标签中加入如下代码:

 <Context path="/pro" privileged="true"
         docBase="E:/work2/alemis2/target/pro">
 </Context>

在项目的web.xml文件的最后面加了如下代码:

<resource-ref>
     <res-ref-name>dataSource</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
 </resource-ref>

然后在spring的application.xml文件中如下引用:

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

我启动tomcat的时候出现了如下错误:

2008-12-09 15:34:29,252 WARN Thread-1 hibernate.cfg.SettingsFactory Could not obtain connection metadata
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
        at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
        at $Proxy0.getConnection(Unknown Source)
        at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82)
        at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
        at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2073)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1298)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367