如何在WildFly中将数据源资源引用名称映射到EJB中的JNDI名称?
我知道在JBoss 4.2.3中它是在jboss.xml
中完成的,但它在WildFly中不起作用。
这是我的测试:
我正在使用Eclipse Luna中的Java 1.7,XDoclet 1.2.3,WildFly 8.2.1.Final,Dynamic Web Module 2.5,EJB 2.1。
在WildFly中,我有一个名为SchedulerDS
的数据源,其JNDI名称为java:jboss/datasources/SchedulerDS
。它使用jTDS JDBC驱动程序并连接到MS SQL Server 2008 R2数据库。
在Eclipse中我创建了这些项目:
在Z01_EJB项目中,我创建了一个名为SLS01
的XDoclet无状态会话Bean(EJB 2.1)。在其中我定义了一个名为jdbc/chumbo
的DataSource资源引用。我将其映射定义为JNDI名称java:jboss/datasources/SchedulerDS
。
这是ejb-jar.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1">
<description><![CDATA[Z01_EJB generated by eclipse wtp xdoclet extension.]]></description>
<display-name>Z01_EJB</display-name>
<enterprise-beans>
<session id="Session_SLS01">
<description><![CDATA[An EJB named SLS01]]></description>
<display-name>SLS01</display-name>
<ejb-name>SLS01</ejb-name>
<home>com.z01.ejb.SLS01Home</home>
<remote>com.z01.ejb.SLS01</remote>
<local-home>com.z01.ejb.SLS01LocalHome</local-home>
<local>com.z01.ejb.SLS01Local</local>
<ejb-class>com.z01.ejb.SLS01Session</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="ResRef_1">
<res-ref-name>jdbc/chumbo</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</session>
</enterprise-beans>
<assembly-descriptor id="AssemblyDescriptor_1">
</assembly-descriptor>
<ejb-client-jar>Z01_EJBClient.jar</ejb-client-jar>
</ejb-jar>
这是jboss.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
<enterprise-beans>
<session>
<ejb-name>SLS01</ejb-name>
<jndi-name>SLS01</jndi-name>
<local-jndi-name>SLS01Local</local-jndi-name>
<resource-ref>
<res-ref-name>jdbc/chumbo</res-ref-name>
<jndi-name>java:jboss/datasources/SchedulerDS</jndi-name>
</resource-ref>
<method-attributes>
</method-attributes>
</session>
</enterprise-beans>
<assembly-descriptor>
</assembly-descriptor>
<resource-managers>
</resource-managers>
</jboss>
在EJB中我编写了一个测试方法
public void testRetrieve() {
String dataSourceLookupString = null;
InitialContext initialContext = null;
DataSource dataSource = null;
Connection connection = null;
String sql = null;
Statement statement = null;
ResultSet resultSet = null;
long employeeId = -1;
try {
dataSourceLookupString = "java:comp/env/jdbc/chumbo";
initialContext = new InitialContext();
dataSource = (DataSource) initialContext.lookup(dataSourceLookupString);
connection = dataSource.getConnection();
sql = "select top 1 employee_id from employee";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet != null && resultSet.next()) {
employeeId = resultSet.getLong(1);
System.out.println("testRetrieve(): employeeId=" + employeeId);
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
它没有用,我收到了这个错误:
2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6) org.h2.jdbc.JdbcSQLException:表&#34;员工&#34;未找到; SQL 声明:2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题 1-6)从员工[42102-173] 2015-12-14中选择前1名employee_id 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.message.DbException.getJdbcSQLException(DbException.java:331) 2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.message.DbException.get(DbException.java:171)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.message.DbException.get(DbException.java:148)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.readTableOrView(Parser.java:4864)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.readTableFilter(Parser.java:1107)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1713) 2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parseSelectSimple(Parser.java:1821)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parseSelectSub(Parser.java:1707)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parseSelectUnion(Parser.java:1550)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parseSelect(Parser.java:1538)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parsePrepared(Parser.java:405)2015-12-14 18:54:50,403 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parse(Parser.java:279)2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.parse(Parser.java:251)2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.command.Parser.prepareCommand(Parser.java:218)2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.engine.Session.prepareLocal(Session.java:428)2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.engine.Session.prepareCommand(Session.java:377)2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138) 2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:72) 2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at org.jboss.jca.adapters.jdbc.WrappedStatement.executeQuery(WrappedStatement.java:344) 2015-12-14 18:54:50,419 ERROR [stderr](MSC服务主题1-6)at com.z01.ejb.SLS01Bean.testRetrieve(SLS01Bean.java:197)
现在我将代码更改为直接查找JNDI名称:
dataSourceLookupString = "java:jboss/datasources/SchedulerDS";
它完美无缺。所以这意味着jboss.xml
在我的情况下不起作用。在这种情况下,如何将DataSource资源引用名称映射到JNDI名称?
由于
答案 0 :(得分:0)
弄清楚我必须使用jboss.xml
代替jboss-ejb3.xml
进行WildFly。