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
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.
K. Tsang CEng MBCS PMP PMI-ACP OCMJEA OCPJP
posted 5 years ago
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?
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.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.