wood burning stoves 2.0*
The moose likes JDBC and the fly likes Connect DS with JNDI in JBoss AS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connect DS with JNDI in JBoss AS" Watch "Connect DS with JNDI in JBoss AS" New topic
Author

Connect DS with JNDI in JBoss AS

Hari Dhanakoti
Ranch Hand

Joined: Jan 07, 2008
Posts: 74
I had been trying to connect databases with JBoss4.2.2 thru JNDI. I had created a new xml file to locate the datasource to connect in the deploy directory. I have a problem of how to locate the JNDI file and how to establish the connection.

Here is my code of MSSQL-DS.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- =========================================================== -->
<!-- -->
<!-- DataDirect Data Sources -->
<!-- -->
<!-- =========================================================== -->
<!--
See the generic_ds.xml file in the doc/examples/jca folder
for examples of properties and other tags you can specify
in data sources
-->
<datasources>
<!-- JBossTest Data Source -->
<local-tx-datasource>
<jndi-name>ds/personDS</jndi-name>
<connection-url>jdbc:datadirect:sqlserver://myserver:1433</connection-url>
<driver-class>com.ddtek.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name>sa</user-name>
<password>123123</password>
<!-- Driver Specific Options -->
<connection-property name="sendStringParametersAsUnicode">false</connection-property>
<!--pooling parameters-->
<min-pool-size>5</min-pool-size>
<max-pool-size>100</max-pool-size>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>15</idle-timeout-minutes>
</local-tx-datasource>
</datasources>


Here i try to connect thru JNDI in java code as

public void getConnection(){
InitialContext cxt = InitialContext.lookup("java:/MSSQLDS");
-----
-----
}
I am sure with the first step of the connection establishment but how to establish connection from this piece of code.


Regards,
Hari
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

Wing,
Welcome to JavaRanch!

First you create an empty initial context. JBoss maps this to the JNDI. Then you lookup the datasource. The JNDI name comes from the XML, not the filename. I see a personDS in your example, but not a MSSQLDS one. Then you can get the connection from the datasource.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connect DS with JNDI in JBoss AS