我在两个Postgresql数据库上的Web应用程序(principalDB和backupDB)中有两个数据源,还有一个Web容器管理事务管理器(带Atomikos)。Spring FW和Hibernate是我的应用程序构建块。我遇到的问题是Jetty 6.1.3 web容器似乎没有加载声明资源的特定于应用程序的web-INF/Jetty-env.xml,因此出现异常:

Caused by: javax.naming.NameNotFoundException; remaining name 'env/jdbc/principalDB'
 at org.mortbay.naming.NamingContext.lookup(NamingContext.java:634)
 at org.mortbay.naming.NamingContext.lookup(NamingContext.java:665)
 at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
 at org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112)
 at javax.naming.InitialContext.lookup(InitialContext.java:351)
 at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
 at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
 at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153)
 at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
 at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
 at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
 at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200)
 at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
 ... 43 more

Here is how I configured the two datasources

  1. in the WEB-INF/web.xml I have declared the two resources with resource-ref as:

    <resource-ref>
     <description>The principal datasource</description>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
     <res-ref-name>jdbc/principalDB</res-ref-name>
    </resource-ref>
    <resource-ref>
     <description>The backup datasource</description>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
     <res-ref-name>jdbc/backupDB</res-ref-name>
    </resource-ref>
    

    在我的
    <New id="principalDB" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg><Ref id="wac"/></Arg>
      <Arg>jdbc/principalDB</Arg>
      <Arg>
        <New class="com.atomikos.jdbc.nonxa.NonXADataSourceBean">
          <Set name="driverClassName">org.postgresql.jdbc3.Jdbc3ConnectionPool</Set>
          <Set name="ServerName">localhost</Set>
          <Set name="PortNumber">5432</Set>
          <Set name="DatabaseName">first</Set>
          <Set name="Url">jdbc:postgresql://localhost:5432/first</Set>
          <Set name="user">test</Set>
          <Set name="password">password</Set>
        </New>
      </Arg>
    </New>
    
    <New id="backupDB" class="org.mortbay.jetty.plus.naming.Resource">
    <Arg><Ref id="wac"/></Arg>
      <Arg>jdbc/backupDB</Arg>
      <Arg>
        <New class="com.atomikos.jdbc.nonxa.NonXADataSourceBean">
          <Set name="driverClassName">org.postgresql.jdbc3.Jdbc3ConnectionPool</Set>
          <Set name="ServerName">localhost</Set>
          <Set name="PortNumber">5432</Set>
          <Set name="DatabaseName">second</Set>
          <Set name="Url">jdbc:postgresql://localhost:5432/second</Set>
          <Set name="user">testSec</Set>
          <Set name="password">password</Set>
        </New>
      </Arg>
    </New>
    

    我做错什么了?

    最佳答案

    确保遵循http://docs.codehaus.org/display/JETTY/Atomikos的步骤1和步骤2(假设您使用的是Atomikos 3.3及更高版本)。
    然后,对于步骤3,请特别注意以下注释:
    由于NonXADataSourceBean只使用java.sql.Driver的类名和url,因此可以将其与提供JDBC驱动程序的任何数据库一起使用。
    因此,您当前的设置包含了太多内容,但更重要的是,驱动程序类名看起来不正确,应该是org.postgresql.Driver
    但是PostgreSQL JDBC驱动程序确实支持XADatasource(使用org.postgresql.xa.PGXADataSource实现),所以我宁愿配置AtomikosDataSourceBean(步骤3的第一个选项)。像这样的:

    <New id="mydatasource" class="org.mortbay.jetty.plus.naming.Resource">
      <Arg><Ref id='wac'/></Arg>
      <Arg>jdbc/mydatasource</Arg>
      <Arg>
        <New class="com.atomikos.jdbc.AtomikosDataSourceBean">
          <Set name="minPoolSize">2</Set>
          <Set name="maxPoolSize">20</Set>
          <Set name="xaDataSourceClassName">org.postgresql.xa.PGXADataSource</Set>
          <Set name="xaProperties">
            <New class="java.util.Properties">
              <Call name="setProperty"><Arg>databaseName</Arg><Arg>testdb</Arg></Call>
              <Call name="setProperty"><Arg>serverName</Arg><Arg>localhost</Arg></Call>
              <Call name="setProperty"><Arg>portNumber</Arg><Arg>5432</Arg></Call>
              <Call name="setProperty"><Arg>user</Arg><Arg>test</Arg></Call>
              <Call name="setProperty"><Arg>password</Arg><Arg>p4ssw0rd</Arg>/Call>
            </New>
          </Set>
          <Set name="UniqueResourceName">mydatasource</Set>
        </New>
      </Arg>
    </New>
    

10-01 07:10
查看更多