I am new to JNDI, JTA, javax.sql though I am familiar with java.sql

I want to access a database (a Derby database) using JNDI lookup and UserTransaction (as I want to learn about Distributed DB Transactions).

I am using Sun Java Application Server(SJAS) 8.2
Database: Derby
Directory Services of : default from SJAS admin console

I have successfully created a connection pool and added the JDBC resource at the admin console of SJAS. I could successfully ping it.

But when I try to execute the following code it throws a exception

Code:

import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
import javax.transaction.*;

public class DerbyDB_dist
{
	public static void main(String args[])
	{

	try
	{
		Properties prop=new Properties();
		prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");				
		Context initialContext = new InitialContext(prop);

		DataSource ds = (DataSource)initialContext.lookup("jdbc/testdb1");
		UserTransaction ut = (UserTransaction)initialContext.lookup("javax.transaction.UserTransaction");
		ut.begin();


		Connection con = ds.getConnection();
		Statement stmt = con.createStatement();
	
		ResultSet rs = stmt.executeQuery("SELECT * FROM abc");
		while (rs.next()) 
		{
			int x = rs.getInt(1);
			String s = rs.getString(2);
			System.out.println("["+x+"]\t["+s+"]");
		}
		con.close();


		ut.commit();

	}
	catch(Exception e)
	{
		e.printStackTrace();
	}

	}
}

Output:

javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
        at com.sun.jndi.ldap.Connection.<init>(Connection.java:204)
        at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:118)
        at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1578)
        at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2596)
        at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:283)
        at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:53)
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
        at javax.naming.InitialContext.init(InitialContext.java:223)
        at javax.naming.InitialContext.<init>(InitialContext.java:197)
        at DerbyDB_dist.main(DerbyDB_dist.java:16)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:516)
        at java.net.Socket.connect(Socket.java:466)
        at java.net.Socket.<init>(Socket.java:366)
        at java.net.Socket.<init>(Socket.java:179)
        at com.sun.jndi.ldap.Connection.createSocket(Connection.java:346)
        at com.sun.jndi.ldap.Connection.<init>(Connection.java:181)
        ... 10 more

I know I messed up somewhere with the JNDI InitialContext thing, but don't know how to sort out the problem.

Please tell me where did I made a mistake? If possible please give some link to any good tutorial explaining this aspect of jndi/jdbc/jta

Weird port number, the default is 3700 (and probably not LDAP).

The following settings are all that's AFAIK required:
jvmarg value = "-Dorg.omg.CORBA.ORBInitialHost=${ORBhost}"
jvmarg value = "-Dorg.omg.CORBA.ORBInitialPort=${ORBport}"

(either as properties of environment settings).

Sorry jwenting, your solution didn't worked.

Can you please tell me whether there is any LDAP server provided with Sun Java Application Server 8.2 or not ? If not how do I connect a LDAP server (e.g. OpenLDAP) with SJAS 8.2 ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.