Problem in creating connection while using JUnit in WSAD

Hi,

I am getting the following exception when the application interact with the oracle database while running JUnit test cases.

***************** Original Exception that the caught exception wrapped:

com.deere.u90.iaf.jdbc.connection.ConnectionException: not initialized

IAF JDBC Connection Management Framework error acquiring connection.

Source of configuration data = C:\starteam\JDPS - Software Delivery system\3123_CanDB\sds\WebContent\WEB-INF\classes\config\SDSLocal.properties

Property Name Suffix = SDS

Dynamic Properties= false

Connection Pooling Enabled= true

JNDI Provider URL= corbaloc:iiop:wpuds90700.jdnet.deere.com:2811

DataSource Name = jdbc/SDSDEVLDataSource

UserId= sdscan

DataSource.toString()= com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource@b6f2dab

Causes:

getConnection caught a ConnectionException.

Consequenses:

The unit of work being processed by this thread can not continue.

at com.deere.u90.iaf.persistence.sqldatabaseaccess.DatabaseAccess.getConnection(DatabaseAccess.java:135)

at com.deere.u90.iaf.persistence.sqldatabaseaccess.DatabaseAccess.<init>(DatabaseAccess.java:43)

at sds.persistence.brokers.DatabaseManager.GetDatabaseAccess(DatabaseManager.java:31)

at sds.programmedecu.persistence.brokers.ProgrammedECUAddComponentBroker.getControllersAssociatedToVehicleSystem(ProgrammedECUAddComponentBroker.java:504)

at junittest.ProgrammedECUAddComponentTest.testGetControllersAssociatedToVehicleSystem(ProgrammedECUAddComponentTest.java:95)

at java.lang.reflect.Method.invoke(Native Method)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at junit.framework.TestSuite.runTest(TestSuite.java:208)

at junit.framework.TestSuite.run(TestSuite.java:203)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)

***************** End of Original Exception.

ContextInfo: Could not get the Vehicle System information.

***************** End of Context Information.

at sds.programmedecu.persistence.brokers.ProgrammedECUAddComponentBroker.getControllersAssociatedToVehicleSystem(ProgrammedECUAddComponentBroker.java:551)

at junittest.ProgrammedECUAddComponentTest.testGetControllersAssociatedToVehicleSystem(ProgrammedECUAddComponentTest.java:95)

at java.lang.reflect.Method.invoke(Native Method)

at junit.framework.TestCase.runTest(TestCase.java:154)

at junit.framework.TestCase.runBare(TestCase.java:127)

at junit.framework.TestResult$1.protect(TestResult.java:106)

at junit.framework.TestResult.runProtected(TestResult.java:124)

at junit.framework.TestResult.run(TestResult.java:109)

at junit.framework.TestCase.run(TestCase.java:118)

at junit.framework.TestSuite.runTest(TestSuite.java:208)

at junit.framework.TestSuite.run(TestSuite.java:203)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)

Can anyone tell the way out .

Thanks in advance,

Dhir

Message was edited by:

Dhir

[3874 byte] By [Dhira] at [2007-11-27 5:03:55]
# 1
Where and how do you create connection in your JUnit test code?
eadelarosaa at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 2

I am trying to integrate Junit with Legacy code in WSAD 5.1. I have a method getConnection that uses datasource to get a connection with the Websphere application server. I have some other methods that call this method. If those methods are called by other classes or a servlet running on the server then everything goes fine. However, when any of those methods are called by a Junit method, then getConnection fails. My JUnit tests also reside within the same web application workspace in WSAD.

Dhira at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 3

That's the problem. The reason why your JUnit test case is failing is that because it runs outside of the ServletContext or JNDI context.

Your servlet or other classes are part of the server's environment context and that's the reason why it's able to get a connection from a DataSource (which I'm sure is bound by JNDI - and acquired through JNDI as well).

In order for your JUnit to get a valid connection under the same environment, you need to use the JNDI context to lookup your DataSource and get a connection from there.

WSAD provides different JNDI environment properties that you need to pass as parameter in order to get an object InitialContext. I'm just not sure if WSAD offers a proxy DataSource or proxy Connection that BEA Weblogic does.

eadelarosaa at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks a lot eadelarosa.Please tell me how should I create my own connection using Datasource for my aplication.If WSAD offers a proxy DataSource than how to use it. I am looking into it whether it offers a proxy Datasource or Connection.
Dhira at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 5

Below is a good template that I can give to you. You will need to identify the corresponding class and package name that WSAD needs to use to service remote client.

This is based on client application connecting to JBoss for illustration purposes and this example is working in JBoss. Everything should be transparent, meaning if you replace <place_holder> with the right values, you should be able to connect to WSAD JNDI remotely and get a connection.

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Hashtable;

import javax.naming.Binding;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import junit.framework.TestCase;

public class TestRemoteJNDIAccess extends TestCase {

private static Logger log = Logger.getLogger(TestRemoteJNDIAccess.class.getName());

DataSource ds = null;

public TestRemoteJNDIAccess(String name) {

super(name);

}

protected void setUp() throws Exception {

super.setUp();

Hashtable env = new Hashtable();

Context ctx = null;

// env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

env.put(Context.INITIAL_CONTEXT_FACTORY, "<change this with WSAD counterpart>");

// env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

env.put(Context.PROVIDER_URL, "<change this with WSAD counterpart>");

// env.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces.jnp:org.jboss.naming:org.jnp.interfaces");

env.put(Context.URL_PKG_PREFIXES, "<change this with WSAD counterpart>");

try {

ctx = (InitialContext) new InitialContext(env);

ds = (DataSource) ctx.lookup("your/datasource/jndiname");

} catch (NamingException ne) {

ne.printStackTrace();

}

}

protected void tearDown() throws Exception {

super.tearDown();

}

public void testConnectionIsAvailable () throws Exception {

Connection conn = null;

try {

conn = ds.getConnection();

log.info("Value of connection: " + conn);

} catch (SQLException sqle) {

log.info("Error in getting connection: " + sqle.getMessage());

} finally {

conn.close();

}

}

}

eadelarosaa at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 6

Thanks for the code but the problem is solved either way.

This was related to JRE which was used by server and client. As WSAD uses IBM JRE and client runs on its own JRE, i.e. SUN JRE, so when it goes for transforming the class it gives this error. This was overcomed by selecting Websphere v5 JRE via window->preferences->java->Installer JRE ->Websphere v5 JRE.

Dhira at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 7
Either way, it's good to hear that you solved the problem.Cheers!
eadelarosaa at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...
# 8
Hi eadelarosa,Can you let me know how to create/instantiate proxy Connection in Weblogic. Appreciate your help.
kkdte1a at 2007-7-12 10:22:07 > top of Java-index,Java Essentials,Java Programming...