Jetty 数据源与 Atomikos UserTransaction

发布于 2024-08-23 15:06:48 字数 3417 浏览 2 评论 0原文

我的 Web 应用程序中有两个数据源(principalDB 和 backupDB),位于两个 Postgresql DB 上,还有一个 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

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

  1. WEB-INF/web.xml中,我已使用resource-ref声明了两个资源:

    <资源引用>
     <描述>主要数据源
     javax.sql.DataSource
     容器
     jdbc/principalDB;
    
    <资源引用>
     <描述>备份数据源
     javax.sql.DataSource
     容器
     jdbc/backupDB;
    
    
  2. WEB-INF/jetty- env.xml 我有

    <新 id="principalDB" class="org.mortbay.jetty.plus.naming.Resource">
    
      jdbc/principalDB;
      
        <新类=“com.atomikos.jdbc.nonxa.NonXADataSourceBean”>
          <设置名称=“driverClassName”>org.postgresql.jdbc3.Jdbc3ConnectionPool
          <设置名称=“服务器名称”>本地主机
          <设置名称=“端口号”>5432
          <设置名称=“数据库名称”>第一个
          <设置名称=“Url”>jdbc:postgresql://localhost:5432/first
          <设置名称=“用户”>测试
          <设置名称=“密码”>密码
        
      
    
    
    <新 id="backupDB" class="org.mortbay.jetty.plus.naming.Resource">
    
      <参数>jdbc/backupDB
      
        <新类=“com.atomikos.jdbc.nonxa.NonXADataSourceBean”>
          <设置名称=“driverClassName”>org.postgresql.jdbc3.Jdbc3ConnectionPool
          <设置名称=“服务器名称”>本地主机
          <设置名称=“端口号”>5432
          <设置名称=“数据库名称”>第二个
          <设置名称=“Url”>jdbc:postgresql://localhost:5432/second
          <设置名称=“用户”>testSec
          <设置名称=“密码”>密码
        
      
    
    

我做错了什么?

I have two datasources in my Web application (principalDB and backupDB) on two Postgresql DBs, and a web container managed transaction manager (with Atomikos) for them. Spring FW and Hibernate are my building blocks for the application. The problem I am running into is that Jetty 6.1.3 web container does not seem to load the app specific WEB-INF/jetty-env.xml that declares the resources so I am getting an exception:

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>
    
  2. in the WEB-INF/jetty-env.xml I have

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-柠檬树下少年和吉他 2024-08-30 15:06:48

确保您遵循了http://docs.codehaus.org/display/JETTY/Atomikos(假设您使用的是 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>

Make sure you followed Step 1 and Step 2 of http://docs.codehaus.org/display/JETTY/Atomikos (assuming you are using Atomikos 3.3 and onwards).

Then, for the Step 3, pay a special attention to this note:

as the NonXADataSourceBean uses only the class name and url of a java.sql.Driver, you can use it with any database providing a JDBC driver.

So your current setup contains too much things but, more important, the driver class name looks wrong, it should be org.postgresql.Driver.

But the PostgreSQL JDBC driver does support XADatasource (with the org.postgresql.xa.PGXADataSource implementation) so I'd rather configure a AtomikosDataSourceBean (the first option of Step 3). Something like that:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文