Configuring Connections Using JNDI


To create your own data source object for JNDI to use with DataDirect XQuery, you can use the example named JNDIDataSource in the /examples subdirectory in your DataDirect XQuery installation directory as a template.

Once you have created a data source object, you can register it with JNDI, as shown in the following XQJ code, where holdings_ds is the name of the data source object:

DDXQDataSource ds = new DDXQDataSource(); 
Context ctx = new InitialContext(); 
ctx.bind("holdings_ds", ds); 

The following XQJ code shows how to load a DDXQDataSource object from JNDI:

Context ctx = new InitialContext(); 
XQDataSource ds = (XQDataSource)ctx.lookup("holdings_ds"); 
XQConnection conn = ds.getConnection("myuserid", "mypswd"); 

In this example, the JNDI environment is first initialized. Next, the initial naming context is used to find the name of the data source object (holdings_ds). The Context.lookup() method returns a reference to a Java object, which is cast to a com.ddtek.xquery.xqj.DDXQDataSource object. Finally, the getConnection() method is called to establish the connection.