java_kevin
asked on
java.sql.SQLException: java.lang.ClassCastException
Hi guys,
Wonder if any help can be rendered.
=====================
Connection conn;
PreparedStatement ps;
conn = null;
ps = null;
InitialContext initCtx = new InitialContext(env); // env has been initialised properly
DataSource ds = (DataSource)initCtx.lookup (dsJndi); // dsJndi has been initialised, but not sure if properly
conn = ds.getConnection();
String query = "SELECT * FROM TLWB_AHP_SESSIONS WHERE (STATUS = '0' OR STATUS = '00') AND TRANSACTION_DATE LIKE ? ORDER BY SESSION_ID ASC";
ps = conn.prepareStatement(quer y);
======================
Upon reaching the last line, an error was thrown:
java.sql.SQLException: java.lang.ClassCastExcepti on.
I tried to print out the value of conn.prepareStatement(quer y) but got the error as well. Does this error mean that prepareStatement is unable to accept "query" properly? I read that, this method throws SQLException when database access error occurs. But this error is ClassCastException. Does it have anything to do with database access? Or somehow, binding is not recognised?
Wonder if any help can be rendered.
=====================
Connection conn;
PreparedStatement ps;
conn = null;
ps = null;
InitialContext initCtx = new InitialContext(env); // env has been initialised properly
DataSource ds = (DataSource)initCtx.lookup
conn = ds.getConnection();
String query = "SELECT * FROM TLWB_AHP_SESSIONS WHERE (STATUS = '0' OR STATUS = '00') AND TRANSACTION_DATE LIKE ? ORDER BY SESSION_ID ASC";
ps = conn.prepareStatement(quer
======================
Upon reaching the last line, an error was thrown:
java.sql.SQLException: java.lang.ClassCastExcepti
I tried to print out the value of conn.prepareStatement(quer
print out and check "dsJndi" value before initCtx.lookup(dsJndi);
How you setup your datasource, admin did it on server or you have data-sources.xml to configure it?
The datasource location (jndiName usually like jdbc/pool/xxxDS) should be passed to lookup();
How you setup your datasource, admin did it on server or you have data-sources.xml to configure it?
The datasource location (jndiName usually like jdbc/pool/xxxDS) should be passed to lookup();
ASKER
Hi hoomanv,
the stack trace:
========
java.sql.SQLException: java.lang.ClassCastExcepti on
at weblogic.jdbc.rmi.SerialCo nnection.p repareStat ement(Seri alConnecti on.java:19 5)
at batchbill.LWBDataExtractor .process(L WBDataExtr actor.java :50)
at batchbill.BillingAgent.pro cess(Billi ngAgent.ja va:89)
at batchbill.BillingAgent.mai n(BillingA gent.java: 191)
==========
Hi owenli,
Even though the dsJndi value has been set in a config file, I should try to print it out first? The datasource is set by our DB team. And the value has been verified by the DB to be correct.
And the funny thing is, when I run this program for the first time in the day, there is the error. For the 2nd run, there is not.
the stack trace:
========
java.sql.SQLException: java.lang.ClassCastExcepti
at weblogic.jdbc.rmi.SerialCo
at batchbill.LWBDataExtractor
at batchbill.BillingAgent.pro
at batchbill.BillingAgent.mai
==========
Hi owenli,
Even though the dsJndi value has been set in a config file, I should try to print it out first? The datasource is set by our DB team. And the value has been verified by the DB to be correct.
And the funny thing is, when I run this program for the first time in the day, there is the error. For the 2nd run, there is not.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi owen,
Thanks for the advice.
I printed out debug statements and it seemed that the Datasource obtained differs each time the program is run.
When it runs fine, the DS is valueA.
When it fails, the DS is valueB. Although each and everytime it runs, the DS should stay the same.
Thanks for the advice.
I printed out debug statements and it seemed that the Datasource obtained differs each time the program is run.
When it runs fine, the DS is valueA.
When it fails, the DS is valueB. Although each and everytime it runs, the DS should stay the same.
ASKER
Now I'm pending to check with my DB team on it...
Kevin
So, there could be two dataSources use same JNDI name(but they may or may not have same dataSource name). The one wrong one lead you to connect different database server but it will fail on permission problem.
Owen
So, there could be two dataSources use same JNDI name(but they may or may not have same dataSource name). The one wrong one lead you to connect different database server but it will fail on permission problem.
Owen
ASKER
Hi Owen,
You've got a point there.
But does it sound odd/funny, that sometimes, it gets the correct datasource, and another time it gets the wrong one? We are using clustered server, by the way, and the program is only migrated to one server. But this "migrate to only 1 server" method is used by other programs as well, and working fine..
You've got a point there.
But does it sound odd/funny, that sometimes, it gets the correct datasource, and another time it gets the wrong one? We are using clustered server, by the way, and the program is only migrated to one server. But this "migrate to only 1 server" method is used by other programs as well, and working fine..
ASKER
Hi owen,
But shouldn't jndi be unique for each connection?
But shouldn't jndi be unique for each connection?
Hi Kevin
I created DataSouces connection on my WSAD server by myself. It may be different environment from your DB team.
For me, both dsName and jndiName must be unique, but it is Container-unique.
(dsName: an identifier to name of a data source)
(jndiName: that data source should be bound to)
Owen
I created DataSouces connection on my WSAD server by myself. It may be different environment from your DB team.
For me, both dsName and jndiName must be unique, but it is Container-unique.
(dsName: an identifier to name of a data source)
(jndiName: that data source should be bound to)
Owen
ex.printStackTrace()