Reputation: 845
I have created a connection pool in Glassfish 3.1.2 called 'userdb' and a Datasource called 'userdbresource'. While creating the datasource I have given the type as 'javax.sql.Datasource' in the admin console.
In my REST webservice I have written the following code
DataSource ds = null;
try
{
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("userdbresource");
}
catch (NamingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con = (Connection) ds.getConnection();
Statement stmt=con.createStatement();
I always get a ClassCast exception at Connection con = (Connection) ds.getConnection();
I have added the following to the web.xml
<resource-ref>
<res-ref-name>userdbresource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
But no change. I have tried to unwrap the connection after I look it up using com.mysql.jdbc.Connection. But no change.
The actual classcast exception in logs is
java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.ConnectionHolder40 cannot be cast to com.mysql.jdbc.Connection
If anyone knows what changes I need to make to use the connection i retrieve from the pool please let me know!!
Thanks Kavita
Upvotes: 0
Views: 703
Reputation: 575
import java.sql.Connection;
not jdbc connection thus you wont need any connection casting
and your datasource connection becomes
Connect con = ds.getConnection();
Upvotes: 2