Я запускаю JBoss 5.1.0 на RHEL 6.5.
Используя консоль администратора JBoss, я создал источник данных XA. В результате был создан файл /opt/jboss-5.1.0.GA/server/default/deploy/myDS-ds.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasources>
<xa-datasource>
<jndi-name>myDS</jndi-name>
<rar-name>jboss-xa-jdbc.rar</rar-name>
<use-java-context>false</use-java-context>
<connection-definition>javax.sql.DataSource</connection-definition>
<jmx-invoker-name>jboss:service=invoker,type=jrmp</jmx-invoker-name>
<min-pool-size>2</min-pool-size>
<max-pool-size>10</max-pool-size>
<blocking-timeout-millis>10000</blocking-timeout-millis>
<idle-timeout-minutes>30</idle-timeout-minutes>
<prefill>false</prefill>
<background-validation>false</background-validation>
<background-validation-millis>0</background-validation-millis>
<validate-on-match>true</validate-on-match>
<statistics-formatter>org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter</statistics-formatter>
<isSameRM-override-value>false</isSameRM-override-value>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>5000</allocation-retry-wait-millis>
<security-domain xsi:type="securityMetaData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<metadata/>
<type-mapping>DB2</type-mapping>
<user-name>db2inst1</user-name>
<password>mypassword</password>
<check-valid-connection-sql>SELECT * FROM ACC.SETTINGS 1=2</check-valid-connection-sql>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
<share-prepared-statements>false</share-prepared-statements>
<set-tx-query-timeout>false</set-tx-query-timeout>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<xa-datasource-class>com.ibm.db2.jcc.DB2XADataSource</xa-datasource-class>
<xa-datasource-property name="DatabaseName">MYDB</xa-datasource-property>
<xa-datasource-property name="DriverType">4</xa-datasource-property>
<xa-datasource-property name="Password">mypassword</xa-datasource-property>
<xa-datasource-property name="PortNumber">50000</xa-datasource-property>
<xa-datasource-property name="ServerName">ibmdb2.mydomain.com</xa-datasource-property>
<xa-datasource-property name="User">db2inst1</xa-datasource-property>
<xa-resource-timeout>0</xa-resource-timeout>
</xa-datasource>
</datasources>
В Eclipse я создал проект Java и добавил WEB-INF-> web. xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="2.4"
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/web-app_2_4.xsd">
<display-name>JNDITest</display-name>
<description>Application to test JNDI DataSource</description>
<servlet>
<display-name>JNDI Servlet</display-name>
<servlet-name>JNDIServlet</servlet-name>
<servlet-class>com.mydomain.server.JNDIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JNDIServlet</servlet-name>
<url-pattern>/JNDIServlet</url-pattern>
</servlet-mapping>
</web-app>
Затем я добавил META-INF-> context. xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink name="jdbc/myDS"
global="jdbc/myDS"
auth="Container"
type="javax.sql.DataSource" />
</Context>
Наконец, файл java : JNDIServlet. java
/**
* Servlet implementation class JNDIServlet
*/
//@WebServlet("/JNDIServlet")
public class JNDIServlet extends HttpServlet {
private static final long serialVersionUID = 2L;
/**
* @see HttpServlet#HttpServlet()
*/
public JNDIServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Context ctx = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:jdbc/myDS");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select name, fname from mydb.owner order by ownerid desc limit 5");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.print("<html><body><h2>Owner Details</h2>");
out.print("<table border=\"1\" cellspacing=10 cellpadding=5><thead><tr>");
out.print("<th>Name</th>");
out.print("<th>Father Name</th>");
out.print("</tr></thead><tbody>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>" + rs.getString("name") + "</td>");
out.print("<td>" + rs.getString("fname") + "</td>");
out.print("</tr>");
}
out.print("</tbody></table></body><br/>");
//lets print some DB information
out.print("<h3>Database Details</h3>");
out.print("Database Product: "+con.getMetaData().getDatabaseProductName()+"<br/>");
out.print("Database Driver: "+con.getMetaData().getDriverName());
out.print("</html>");
}catch(NamingException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
ctx.close();
} catch (SQLException e) {
System.out.println("Exception in closing DB resources");
} catch (NamingException e) {
System.out.println("Exception in closing Context");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Однако я не могу установить соединение в строке ctx.lookup. Ошибка: javax.naming.NameNotFoundException: jdb c не привязан
Я пробовал разные комбинации, но не могу выйти за пределы этой точки.
Я разместил все банки db2 файлы в /opt/jboss-5.1.0.GA/common/lib