Multi-threading and DB Connection pool problems

Howdy all,

this is my first post as all can see. I've searched through many pages of the forum and either this hasn't been addressed exactly or I'm asking the wrong question...

The problem is with my web app running on the Sun App Server. I have a process that runs on it's own thread but doesn't use the connection pool that the rest of the app uses.

For some reason the second process doesn't find the name in the context, no idea why. Is this a known problem or is it some type of code problem? I didn't write the code originally, but I am trying to get it to work correctly.

Thanks so much for any help!

-The Electronic PaperClip

Message was edited by:

Electronic.Paper.Clip

[735 byte] By [Electronic.Paper.Clipa] at [2007-11-27 3:22:09]
# 1
Just a note, I am getting javax.naming.NamingException: invocation exception on the context lookup.I tried creating a resource named dbname_pm and made the associated changes, but that didn't have any effect.Thanks,-paper clip
Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
You have a connection pool.You don't want to use it.And then you are attempting to get the name of what? Certainly can't be the connection pool.
jschella at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
jschell,Is that a statement or a question? I do want to use the connection pool for the 2nd thread. The context lookup is on the resource assigned to the connection pool on the sun server.-epc
Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> jschell,

> Is that a statement or a question?

There is a question mark in it.

> I do want to use

> the connection pool for the 2nd thread.

Perhaps you need to rephrase your question then because your first post said the following....

"...doesn't use the connection pool that the rest of the app uses."

jschella at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Ah, sorry for the confusion. What I meant is that it currently is not using the connection pool but it should be. If posting up any code would help let me know. It's pretty standard connection pool code, but the multithreading involves the use of singletons which I am not very familiar with.

Thanks!

EPC

Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

So a connection pool exists and in is use and you can't resolve it in another place in the code?

Or is this a completely brand new pool?

Probably doesn't matter since the most likely source of the problem is that either you are using the wrong name in the code or you do not have it correctly set up in the configuration files.

jschella at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

I'll post up snippets of the code to give examples, but the pool is working for the parent thread, but when a child thread is started, that child thread fails on the context lookup.

domain.xml:

<jdbc-resource enabled="true" jndi-name="jdbc/ship" object-type="user" pool-name="shipdev"></jdbc-resource>

<jdbc-resource enabled="true" jndi-name="jdbc/ship_pm" object-type="user" pool-name="shipdev"/>

<jdbc-connection-pool allow-non-component-callers="false" connection-validation-method="auto-commit"

datasource-classname="oracle.jdbc.pool.OracleConnectionPoolDataSource" fail-all-connections="false" idle-timeout-in-seconds="10"

is-connection-validation-required="false" is-isolation-level-guaranteed="false" max-pool-size="200"

max-wait-time-in-millis="10000" name="shipdev" non-transactional-connections="false" pool-resize-quantity="2"

res-type="javax.sql.ConnectionPoolDataSource" steady-pool-size="8">

<property name="DataSourceName" value="OracleConnectionPoolDataSource"/>

<property name="ImplicitCachingEnabled" value="false"/>

<property name="NetworkProtocol" value="tcp"/>

<property name="LoginTimeout" value="0"/>

<property name="Password" value="ship4"/>

<property name="URL" value="jdbc:oracle:thin:@128.104.172.81:1521:SHIPDEV"/>

<property name="User" value="hpad"/>

<property name="ExplicitCachingEnabled" value="false"/>

<property name="PortNumber" value="0"/>

<property name="MaxStatements" value="0"/>

</jdbc-connection-pool>

web.xml:

<resource-ref>

<description>Ship Database</description>

<res-ref-name>jdbc/ship</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

<resource-ref>

<description>Ship pm</description>

<res-ref-name>jdbc/ship_pm</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

Class that starts a new thread:

/**

* @(#)EventMonitor.java

* *

* */

package edu.wisc.uhs.iecs.event;

import java.util.EmptyStackException;

/**

*

Title: EventMonitor

*

Description: The EventMonitor is responsible for retrieving scheduled

* Events from and putting them on the queue. It provides a mechanism for

* implementing a scheduling policy.

* @version 1.0

*/

public class EventMonitor implements Runnable {

private static EventMonitor m_instance;

private EventProcessor m_processor;

private EventQueue m_queue;

private EventDataAccess m_data;

private Thread m_runner;

//private long INTERVAL = 1000 * 60 * 10; // RUN EVERY 10 MINUTES

private long INTERVAL = 1000 * 1 * 60; // RUN EVERY 60 SECONDS

/**

* Private Constructor.

*/

private EventMonitor() {

System.out.println("EventMonitor is being Initialized.");

m_queue = new EventQueue();

m_processor = EventProcessor.getInstance();

m_processor.setEventQueue(m_queue);

m_data = EventDataAccess.getInstance();

init();

}

/**

* Singleton Model Constructor.

* @return EventMonitor

*/

public static synchronized EventMonitor getInstance() {

if (m_instance == null) {

try {

//System.out.println("Event Monitor.getInstance() being called.");

m_instance = new EventMonitor();

//throw new EmptyStackException();

}catch(Exception e) {

e.printStackTrace();

}

}

return m_instance;

}

/**

* Init.

*/

public void init(){

if (m_runner == null) {

start();

} else {

stop();

}

}

/**

*

* @param _queue EventQueue

*/

public void setEventQueue(EventQueue _queue) {

m_queue = _queue;

}

/**

*

* @return EventQueue

*/

public EventQueue getEventQueue() {

return m_queue;

}

/**

* Start the monitor.

*/

public void start() {

m_runner = new Thread(this);

m_runner.setDaemon(true);

m_runner.start();

//System.out.println("Started Monitor");

}

/**

* Stop the monitor.

*/

public void stop() {

m_runner.interrupt();

m_runner = null;

//System.out.println("Stopped Monitor");

}

/**

* run

*/

public void run() {

try {

while(!Thread.interrupted()) {

doSchedule();

Thread.sleep(INTERVAL);

if (Thread.interrupted()) {

//System.out.println("Monitor has been interrupted");

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Do Scheduling. Query the Database for scheduled events and push them onto the queue

* for processing.

*/

private void doSchedule() {

System.out.println("Monitoring Scheduled Events for " + new java.util.Date());

m_data.loadSchedule(getEventQueue());

}

public static void main(String args[]) {

EventMonitor eMonitor = EventMonitor.getInstance();

}

}

Message was edited by:

Electronic.Paper.Clip

Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

When m_data is loaded it calls

m_connWrapper = ConnectionWrapper.getInstance();

m_data.loadSchedule() = some sql and then

conn = getConnection();

public Connection getConnection() {

Connection conn_ = null;

conn_ = m_connWrapper.getConnection("jdbc/ship_pm");

return conn_;

}

The connection wrapper is as follows:

public static synchronized ConnectionWrapper getInstance() {

if (m_instance == null) {

m_instance = new ConnectionWrapper();

}

return m_instance;

}

public Connection getConnection(String _datasource) {

Connection conn_ = null;

System.out.println("CW - this:"+this);

try {

Context ctx = new InitialContext();

try {

DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + _datasource);

if (ds != null) {

conn_ = ds.getConnection();

}

} catch (Exception e) {

// DO SILENTLY

System.out.println("Context lookup failed");

e.printStackTrace();

}

// AS LAST RESORT

if (conn_ == null) {

if (_datasource.equals(HPAD_DATA_SOURCE)) {

try {

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

System.out.println("Not using connection pool");

conn_ = java.sql.DriverManager.getConnection(Constants.HPAD_CONNECTION, "un", "pw");

} catch (Exception e) {

e.printStackTrace();

}

} else {

try {

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

conn_ = java.sql.DriverManager.getConnection(Constants.DWH_CONNECTION, "un", "pw");

} catch (Exception e) {

e.printStackTrace();

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

return conn_;

}

Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
Anybody have any ideas? (giving myself a bump here) I'm still stumped on why this isn't working, when I do a context binding find it finds the resource names that I am referencing, but when I do the look up it gives an invocation exception...thanks for any help,EPC
Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
worked a bit on this problem yesterday and found that if i change where the call to the JNDI tree is made, then it works! Basically it has nothing to do with threads and all to do with timing.-EPC
Electronic.Paper.Clipa at 2007-7-12 8:24:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...