How to solve the java. Lang. classcastexception problem caused by using a database connection pool circulating on the Internet at proxy. newproxyinstance

Source: Internet
Author: User
The code for getfreeconnection is as follows:
Public connection getfreeconnection (){
// Return the conn class of the database connection to intercept the close method.
Connection conn2 = NULL;
If (conn instanceof connection ){
Conn2 = (connection) proxy. newproxyinstance (conn. getclass (). getclassloader (), conn. getclass (). getinterfaces (), this );
}
Return conn2;
}
There is no problem with the database driver of mysqlv5. When using the database drivers of mysqlv6 and Oracle, the following error is reported: Java. Lang. classcastexception: $ proxy0 cannot be cast to Java. SQL. Connection
At dbpoolimpl. _ connection. getfreeconnection (_ connection. Java: 126)
At dbpoolimpl. connectionfactory. getfreeconnection (connectionfactory. Java: 113)
At dbpoolimpl. database_task.checkdatabase (database_task.java: 83)
At dbpoolimpl. database_task.processhandle (database_task.java: 227)

After searching online for a long time, I finally found a solution and changed the statement for creating the proxy class to "conn2 = (connection) proxy. newproxyinstance (Conn. getclass (). getclassloader (), new class [] {connection. class}, this );

The reason is that conn. getclass (). the getinterfaces () method generates an array of class classes. The first element of this array must be a connection to convert the created proxy class to a connection object, you can use the following statement class [] interfaces = Conn. getclass (). getinterfaces (); For (Class C: interfaces) {system. out. println (C. getcanonicalname ();} print the conn when different database drivers are used. getclass (). what are the elements in getinterfaces ()?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.