Marketplace for Content, Tests and Assessment
 
 
Home > Programming / tutorials > JNDI lookup on JBoss

JNDI lookup on JBoss

There is no standard way as per Java EE specs for  JNDI naming conventions, hence most of the application servers have their own way of JNDI naming.

On specifying a Datasource’s JNDI name as ‘myDatasource’, JBoss binds as ‘java:myDatasource’. So if one want to deploy the application on Jboss application servers then can use the simplest way is here. The steps for adding and using jndi Datasource to JBOSS 4.x is as under-

  • Copy the jdbc driver to JBOSS_HOME/server/default/lib.
  • Create the jndi xml file for datasource – example ds.xml

<?xml version=“1.0″ encoding=“UTF-8″?>

<datasources>

<local-tx-datasource>

<jndi-name>myDataSource</jndi-name>

<connection-url>jdbc:sqlserver://10.0.120.109:1199;databaseName= Systest;lastupdatecount=false;socketKeepAlive=true</connection-url>

<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver

</driver-class>

<min-pool-size>5</min-pool-size>

<max-pool-size>20</max-pool-size>

<user-name>user</user-name>

<password>password</password>

<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>

<metadata>

<type-mapping>MS SQLSERVER2000</type-mapping>

</metadata>

</local-tx-datasource>

</datasources>

  • Copy the ds.xml to JBOSS_HOME/server/default/deploy
  • Now we  look this up using java:jndi-name in our java code as in below example. it Works perfect. Example—

public static Connection GetSqlServerConnection() {

java.sql.Connection conn = null;

try {

Context initContext = new InitialContext();

javax.sql.DataSource ds = (javax.sql.DataSource)initContext.lookup(“java:myDataSource”);

conn = ds.getConnection();

return conn;

}

catch (javax.naming.NamingException nex) {

nex.printStackTrace();

logger.error(nex.getMessage(), nex);

return null;

}

catch (Exception e) {

e.printStackTrace();

logger.error(e.getMessage(), e);

return null;

}

}

Now For compatibility with other containers, we can move the  jndi-datasource to for example  java:comp/ jdbc/jndi-name location then we need to add the <resource-ref> for jndi name to  jboss-web.xml & web.xml.

Categories: Programming / tutorials Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes