Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:29:48+00:00 2026-06-11T11:29:48+00:00

I have created a connection pool in Glassfish 3.1.2 called ‘userdb’ and a Datasource

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:29:49+00:00Added an answer on June 11, 2026 at 11:29 am

    import java.sql.Connection; not jdbc connection thus you wont need any connection casting
    and your datasource connection becomes

    Connect con = ds.getConnection();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the application in which I need to configure the connection pool.In
On a glassfish server I have a JDBC Connection pool set up for my
I have created an ODBC connection for oracle using Microsoft ODBC for Oracle driver
I have created a class that will manage connection and commands, I named it
I have created a service class for my network connection so that my app
I have created an tableview that shows RSSS. I check for internet connection then
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
I have main data source for working with database, which uses some connection pool.
Per this helpful article I have confirmed I have a connection pool leak in
I have created a server in java which accepts client connections. But I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.