【问题标题】:Jetty datasource with Atomikos UserTransaction带有 Atomikos UserTransaction 的 Jetty 数据源
【发布时间】:2011-01-22 00:56:42
【问题描述】:

我的 Web 应用程序中有两个数据源(principalDB 和 backupDB),位于两个 Postgresql DB 上,还有一个用于它们的 Web 容器管理事务管理器(使用 Atomikos)。 Spring FW 和 Hibernate 是我的应用程序构建块。我遇到的问题是 Jetty 6.1.3 Web 容器似乎没有加载声明资源的应用程序特定的 WEB-INF/jetty-env.xml,所以我遇到了一个异常:

引起:javax.naming.NameNotFoundException;剩余名称 'env/jdbc/principalDB' 在 org.mortbay.naming.NamingContext.lookup(NamingContext.java:634) 在 org.mortbay.naming.NamingContext.lookup(NamingContext.java:665) 在 org.mortbay.naming.NamingContext.lookup(NamingContext.java:680) 在 org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112) 在 javax.naming.InitialContext.lookup(InitialContext.java:351) 在 org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155) 在 org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88) 在 org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153) 在 org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 在 org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 在 org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 在 org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200) 在 org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334) ... 43 更多

这是我配置两个数据源的方式

  1. WEB-INF/web.xml 中,我已将具有resource-ref 的两个资源声明为:

    <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>
    
  2. WEB-INF/jetty-env.xml我有

    <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>
    

我做错了什么?

【问题讨论】:

    标签: postgresql jakarta-ee jetty jta atomikos


    【解决方案1】:

    确保确保您遵循了http://docs.codehaus.org/display/JETTY/Atomikos步骤 1步骤 2(假设您使用的是 Atomikos 3.3 及更高版本)。 p>

    然后,对于第 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>
    

    【讨论】:

    • 似乎即使我的 jetty-plus.xml 是一个损坏的 XML(比如有一个未关闭的 '/Call>' 标记),我也得到了与配置文件完全一样的异常被码头忽略。我将 jetty-plus.xml 中的配置复制到 [jetty]/etc/jetty.xml 并更正 XML 数据源仍未向 JNDI 注册。
    • @peter 很难说没有看到你在做什么。但是这些说明docs.codehaus.org/display/JETTY/JNDI 绝对有效。
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2012-07-04
    • 2013-01-08
    • 2011-04-18
    相关资源
    最近更新 更多