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
# 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
# 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_;
}