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 8903009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:45:36+00:00 2026-06-15T01:45:36+00:00

UPDATE solution is Java.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignment

  • 0

UPDATE solution is Java.lang.reflect.Proxy returning another proxy from invocation results in ClassCastException on assignment

My test code proxies java.sql.Connection.

I create my proxy like so:

log.info("connection is "+connection.getClass().getName()+", "+
    (connection instanceof Connection));
Object proxy = java.lang.reflect.Proxy.newProxyInstance(
    connection.getClass().getClassLoader(),
    connection.getClass().getInterfaces(),
    new MockFailureWrapper(connection));
log.info("proxy is "+proxy.getClass().getName()+", "+
    (proxy instanceof Connection));
return (Connection)proxy;

When I wrap an H2 DB connection, this works perfectly.

When I try and wrap a MySQL connection, the cast of the proxy to Connection in the return fails, even though the connection I’m wrapping is of type Connection. The exception is:

java.lang.ClassCastException: $Proxy11 cannot be cast to java.sql.Connection

The log line for an H2 connection is:

connection is org.h2.jdbc.JdbcConnection, true
proxy is $Proxy9, true

And for the MySQL connection:

connection is com.mysql.jdbc.JDBC4Connection, true
proxy is $Proxy11, false

What’s going on, and why can’t I wrap MySQL DB connections?

  • 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-15T01:45:38+00:00Added an answer on June 15, 2026 at 1:45 am

    The problem is this line:

    connection.getClass().getInterfaces()
    

    It just gives you the interfaces that are directly implemented by the class you would like to proxy. If the Connection-interface is implemented f.eg. by a superclass of the MySql-Connection class (lets say AbstractConnection), your proxy will not implement the Connection interface.

    To fix this, add the Connection-Interface to the array of interfaces your proxy should implement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i developed a small java.lang.reflect.InvocationHandler that intercepts method calls (from a json webservice) and
I am using SWIG to access C++ code from Java. What is the easiest
UPDATE: The Code working when I use it as java program. And When I
Update based on @Tom solution: SongsManager.java public class SongsManager extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>
Update Solution Found See Bottom of post if interested Seems simple enough and for
I am not interested in any auto update solution, such as ClickOnce or the
[UPDATE] the clean goal runs smoothely all over the projects of the solution, the
Update: I've added an answer that describes my final solution (hint: the single Expr
NOTE: I added my new solution at the UPDATE answer below. I try to
Update: This question was an epic failure, but here's the working solution. It's based

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.