自由配置文件服务器中的数据源初始化错误 [英] DataSource initialization error within liberty profile server
问题描述
我试图在自由配置文件服务器中设置一个数据源,但是当我的代码尝试使用它时,我得到一个NullPointerException
(在下面的ds变量中).
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.
相关代码和server.xml
条目如下.
有趣的是,如果我将jndiName更改为java:comp/env/jdbc/Oracle
,则在服务器启动时会得到一个IllegalArgumentException
,但是使用下面的配置,它甚至似乎都没有尝试激活数据源...
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;
}
我的server.xml
配置:
<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 ] 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]
该代码在我们的数据访问层的基类中.我们通过非常简单的初始化在RESTful Web服务中调用它:
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();
推荐答案
除非激活CDI,否则@Resource
在POJO中将不起作用.请尝试以下操作:
The @Resource
will not work in POJO unless you activate CDI. Try the following:
- 将bean.xml文件添加到
WEB-INF
文件夹,并添加CDI功能
- add beans.xml file to the
WEB-INF
folder, and add CDI feature
<feature>cdi-1.0</feature>
- 使用
@Inject AuditDAO auditDAO
将auditDAO注入您的Web服务
- 在dao中使用以下参考文献
- 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;
这篇关于自由配置文件服务器中的数据源初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!