我正在尝试通过Wildfly 10上的RestEasy webservice中的@Resource注释访问JNDI Oracle数据源,但返回null。它适用于InitialContext.lookup方法。这是我的java代码:
Default4.aspx.cs
这是我的standalone.xml数据源定义:
@Path("/rest")
public class ServiceTest {
@Resource(name = "jdbc/OracleDS") DataSource source1;
@Resource(name = "java:/jdbc/OracleDS") DataSource source2;
@Resource(lookup = "java:/jdbc/OracleDS") DataSource source3;
@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
public String testJNDI () {
try {
javax.naming.Context initCtx = new InitialContext();
DataSource source4 = (DataSource)initCtx.lookup("java:/jdbc/OracleDS");
initCtx.close();
return "" + source1 + " / " + source2 + " / " + source3 + " / " + source4;
} catch (Exception error) {
e.printStackTrace();
}
}
这是我的module.xml代码:
<datasource jta="true" jndi-name="java:/jdbc/OracleDS" pool-name="OracleDS" enabled="true" use-ccm="true" statistics-enabled="true">
<connection-url>
jdbc:oracle:thin:@192.168.10.20:1521:log
</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<driver>oracle</driver>
<pool>
<initial-pool-size>1</initial-pool-size>
<max-pool-size>10</max-pool-size>
</pool>
<security>
<user-name>user</user-name>
<password>passw</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<background-validation>true</background-validation>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
</validation>
<timeout>
<blocking-timeout-millis>30000</blocking-timeout-millis>
<idle-timeout-minutes>15</idle-timeout-minutes>
<query-timeout>300</query-timeout>
</timeout>
<statement>
<track-statements>nowarn</track-statements>
</statement>
</datasource>
服务输出是:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.oracle">
<resources>
<resource-root path="ojdbc6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
为什么source4创建资源而其他资源没有?我预计至少source3会像source4一样。 @Resource定义中有什么问题吗?
由于
答案 0 :(得分:2)
确保您的WEB-INF目录中有beans.xml
。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>