سؤال

I am migrating from glassfish to embedded jetty. I have configured embedded jetty to have CDI using weld and it works. I would like to connect to AS400 DB2 database and I am having troubles configuring this.

My glassfish-resources.xml looks like this:

<jdbc-connection-pool name="db2_pool"
                      datasource-classname="com.ibm.as400.access.AS400JDBCDataSource"
                      res-type="javax.sql.ConnectionPoolDataSource"
                      driver-classname="">
    <description />
    <property name="DatabaseName" value="xxx"></property>
    <property name="ServerName" value="1.1.1.1"></property>
    <property name="naming" value="SYSTEM"></property>
    <property name="User" value="user"></property>
    <property name="Password" value="pass"></property>
    <property name="URL" value="jdbc:as400://1.1.1.1;libraries=db2"></property>
    <property name="libraries" value="DB2"></property>          
</jdbc-connection-pool>

Does anyone know how to configure jetty-env.xml and web.xml to have the same with jetty 9.1

Thanks!

هل كانت مفيدة؟

المحلول

Assuming Jetty 9.1

See: http://www.eclipse.org/jetty/documentation/current/jndi-configuration.html#configuring-datasources

In your WEB-INF/jetty-env.xml

<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="db2_pool" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jdbc/db2_pool</Arg>
    <Arg>
      <New class="com.ibm.as400.access.AS400JDBCDataSource">
        <Set name="DatabaseName">xxx</Set>
        <Set name="ServerName">1.1.1.1</Set>
        <Set name="naming">SYSTEM</Set>
        <Set name="User">user</Set>
        <Set name="Password">pass</Set>
        <Set name="URL">jdbc:as400://1.1.1.1;libraries=db2"></Set>
        <Set name="libraries">DB2</Set>
      </New>
    </Arg>
  </New>
</Configure>

Then you have a WEB-INF/web.xml section

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

Finally, in your code you access it via ...

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MyClass {

  public void myMethod() {

    InitialContext ic = new InitialContext();
    DataSource myDS = (DataSource)ic.lookup("java:comp/env/jdbc/db2_pool");     

    ...
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top