Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Glassfish Connection Pool -- ClassCastException in Application

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am starting a project using Netbeans, Glassfish 4.1, and MySql 5.6, and I am having trouble trying to get a DataSource object from the server's connection pool. Currently, the database is set up, and the connection pool and JDBC resource is setup. I am trying to use the Connector/J driver downloaded from the MySql site. From the Glassfish console, I am able to ping the connection pool successfully. The attached screenshots show the configuration for the pool, it's pretty much default.

As you'll see, the classname for the 'iusd_helpdesk-pool' is set as 'com.mysql.jdbc.jdbc2.optional.MysqlDataSource'.

Any help would be appreciated!

Problem:
When using the InitialContext().lookup() method, I get an exception when trying to cast the object to the MysqlDataSource type..
java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to com.mysql.jdbc.jdbc2.optional.MysqlDataSource

Why is Glassfish giving me a DataSource object of this type 'com.sun.gjc.spi.jdbc40.DataSource40'? I'm new to this but I was thinking that by specifying the DataSource Classname for the connection pool in Glassfish as 'com.mysql.jdbc.jdbc2.optional.MysqlDataSource', that it would produce this type of DataSource object. I have also copied the mysql connector .jar file into the domain\lib folder in Glassfish.

Attempted Fixes:
I have tried to simply cast it to javax.sql.DataSource and have also turned off wrapping in the Glassfish console. This just causes this exception..
java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.DataHolder40 cannot be cast to com.mysql.jdbc.jdbc2.optional.MysqlDataSource

Code:
pool-general.PNG
[Thumbnail for pool-general.PNG]
pool-advanced.PNG
[Thumbnail for pool-advanced.PNG]
jdbc_resource.PNG
[Thumbnail for jdbc_resource.PNG]
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't necessarily cast to MysqlDataSource, you just cast to DataSource.

Also you can set the username, password, URL in the connection pool rather than using getConnection() in code. That way if connection password changes, no code changes is needed.

I usually use a utility function to get the connection like:



Hope this helps.
 
Wayne Woodrow
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the advice and the fix! I went ahead and changed my DataSource, PreparedStatement, and Connection to objects from the javax.sql.DataSource, java.sql.PreparedStatement, and java.sql.Connection, and the program is working.

I am still wondering though why I cannot use the ones that I was trying to before?
 
Saloon Keeper
Posts: 22630
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you were providing a userid and password on your getConnection method call.

Pools contain LIVE connections. And all those connections are expected to be interchangeable. If you "obtain" a Connection with different userids, then obviously that violates one of the prime attributes of pool connections. And implicitly you're creating (opening) a Connection instead of actually pulling one from the pool, since you'd have no idea at the application level what userid the pool connections are constructed under.
reply
    Bookmark Topic Watch Topic
  • New Topic