• Post Reply Bookmark Topic Watch Topic
  • New Topic

servlets and threads  RSS feed

 
saikiran surya
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've a rather weird problem with threads spawned by my servlet.
I have a servlet say "test" that creates a new thread in its init method. The thread class looksup for the datasource and does some database work.

My problem is, I could successfull lookup for the datasource in my main servlet(test) but in my thread class when I try to lookup the datasource, it says cannot find resource reference. What am I doing wrong.

I'm using websphere application server and WSAD environment.

Appreciate your help,
ss
 
Richard Bradford
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it would be helpfull if you could post some code to give a better idea of what you are trying to achive.
 
saikiran surya
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,

I'm going to provide some code snippets for this problem.

****************************************************************************
Main Servlet "TEST"
****************************************************************************
public void init() throws ServletException {

super.init();


try {

Timer t = new Timer();

t.schedule(new DBThread(), 1000, 60*1000);

} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

catch(SQLException se){

se.printStackTrace();

}

}

****************************************************************************
Back ground DB Thread class
****************************************************************************
public class DBThread extends TimerTask {


public void run(){


System.out.println("I'm in run in DBThread");

Connection conn= null;

Statement stmt = null;

ResultSet rs = null;

try {

java.util.Hashtable env = new java.util.Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

env.put(Context.PROVIDER_URL,"iiop://localhost:2809");

Context ctxt = new InitialContext(env);

/**
*Even tried with a local lookup by not setting the environment my self.
* It did not work either
*/

DataSource ds = (DataSource)ctxt.lookup("jdbc/mysql");

conn = ds.getConnection("","");

stmt = conn.createStatement();

rs = stmt.executeQuery("SELECT * FROM EMPLOYEE");

while(rs.next()){

System.out.println("Contains:" + rs.getString(1));

}


} catch (NamingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

catch(SQLException se){

se.printStackTrace();

}


finally{


try {

if(conn!= null)

conn.close();

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

}

****************************************************************************
Console errors and information
****************************************************************************

[9/29/04 7:54:23:660 EDT] 22fecb7e ConnectionFac I J2CA0122I: Resource reference jdbc/mysql could not be located, so default values of the following are used: [Resource-ref settings]

res-auth: 1 (APPLICATION)

res-isolation-level: 0 (TRANSACTION_NONE)

res-sharing-scope: true (SHAREABLE)

res-resolution-control: 999 (undefined)

[Other attributes]

isCMP1_x: false (not CMP1.x)

isJMS: false (not JMS)

[9/29/04 7:54:23:830 EDT] 22fecb7e ConnectionMan W J2CA0075W: An active transaction should be present while processing method allocateMCWrapper.

[9/29/04 7:54:23:860 EDT] 22fecb7e ConnectionMan W J2CA0075W: An active transaction should be present while processing method initializeForUOW.

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R java.sql.SQLException: General error, message from server: "No Database Selected"

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1962)

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1257)

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1218)

[9/29/04 7:54:24:912 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.Connection.execSQL(Connection.java:2239)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.Connection.execSQL(Connection.java:2187)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.Statement.executeQuery(Statement.java:1158)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at com.mysql.jdbc.jdbc2.optional.StatementWrapper.executeQuery(StatementWrapper.java:617)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at com.ibm.ws.rsadapter.jdbc.WSJdbcStatement.executeQuery(WSJdbcStatement.java:445)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at com.test.DBThread.run(DBThread.java:37)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at java.util.TimerThread.mainLoop(Timer.java:446)

[9/29/04 7:54:24:922 EDT] 22fecb7e SystemErr R at java.util.TimerThread.run(Timer.java:396)
****************************************************************************

Note: That the resouce tab in the web.xml is also set to point to the data-source

Appreciate your help in advance.

SS
 
Richard Bradford
Ranch Hand
Posts: 48
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!