• Post Reply Bookmark Topic Watch Topic
  • New Topic

Glassfish Connection Pool -- ClassCastException in Application  RSS feed

 
Wayne Woodrow
Greenhorn
Posts: 10
  • 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]
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • 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
Greenhorn
Posts: 10
  • 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?
 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!