DataSource initialization error within liberty pro

2019-08-13 02:01发布

I am trying to set up a DataSource in liberty profile server, but I'm getting a NullPointerException (on my ds variable below) when my code tries to use it.

The relevant code, and server.xml entries are below. Interestingly, if I change the jndiName to java:comp/env/jdbc/Oracle I get an IllegalArgumentException on server startup, but with the config below it doesn't even seem to try to activate the DataSource...

public abstract class DAOBase {
    //@Resource(name = "jdbc/Oracle", type = javax.sql.DataSource.class, shareable = true, authenticationType = AuthenticationType.CONTAINER)
    @Resource(lookup = "jdbc/Oracle")
    private DataSource ds;


protected Connection getConnection() throws SQLException {
    Connection conn = ds.getConnection();
    return conn;
}

My server.xml config:

<featureManager>
    <feature>jsp-2.2</feature>
    <feature>jaxrs-1.1</feature>
    <feature>localConnector-1.0</feature>
    <feature>appSecurity-2.0</feature>
    <feature>jpa-2.0</feature>
    <feature>jdbc-4.0</feature>
    <feature>jndi-1.0</feature>
</featureManager>

<library id="OracleJDBC_ID">
    <fileset dir="C:\src\main\lib" includes="ojdbc6.jar"/>
</library>
<jdbcDriver id="OracleDriver" libraryRef="OracleJDBC_ID"/>

<dataSource jdbcDriverRef="OracleDriver" jndiName="jdbc/Oracle">
    <properties.oracle URL="jdbc:oracle:thin:@ldap://oid:***/opl***,cn=OracleContext,dc=****,dc=com" password="****" user="*****"/>
</dataSource>

Error in the log:

[ERROR   ] CWWKE0701E: [com.ibm.ws.jdbc.dataSource(200)] The modified method has
thrown an exception Bundle:com.ibm.ws.jdbc(id=82) java.lang.IllegalArgumentException: 
J2CA8011E: Value java:comp/env/jdbc/Oracle is not supported for jndiName on dataSource
    at com.ibm.ws.jdbc.DataSourceService.activate(DataSourceService.java:209)
    at [internal classes]

EDIT: The code is in the base class for our Data Access layer. We're calling this in a RESTful web service via a very plain initialization:

AuditDAO auditDAO = new AuditDAO();

1条回答
在下西门庆
2楼-- · 2019-08-13 02:58

The @Resource will not work in POJO unless you activate CDI. Try the following:

  • add beans.xml file to the WEB-INF folder, and add CDI feature
<feature>cdi-1.0</feature>
  • inject auditDAO to your web service using @Inject AuditDAO auditDAO
  • use the following reference in the dao
   @Resource(name="jdbc/Oracle", lookup = "jdbc/Oracle")
   private DataSource ds;
查看更多
登录 后发表回答