【问题标题】:c3p0-0.9.5.2 Statement unwrap cause abstractmethoderrorc3p0-0.9.5.2 语句解包导致abstractmethoderror
【发布时间】:2016-11-11 06:31:51
【问题描述】:

我使用 c3p0 版本 0.9.5.1。带有此错误的 unwrap() 方法错误:

java.lang.AbstractMethodError: 方法 com/mchange/v2/c3p0/impl/NewProxyStatement.unwrap(Ljava/lang/Class;)Ljava/lang/Object; 是抽象的 com.mchange.v2.c3p0.impl.NewProxyStatement.unwrap(NewProxyStatement.java)

这是我的代码:

com.mysql.jdbc.Statement stm =  proxyStatement.unwrap(com.mysql.jdbc.Statement.class)

我看到一些帖子说它应该从 0.9.5 开始工作。 它仍然是一个错误吗?

那我如何从 c3p0 Statement/Connection 获取原始 Statement/Connection 呢? 非常感谢

【问题讨论】:

标签: java mysql jdbc connection c3p0


【解决方案1】:

您几乎可以肯定在您的应用程序的有效CLASSPATH 中某处有旧版本的 c3p0。请在 INFO 中查看您的日志以获取 c3p0 的横幅,例如

INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]

为了确定这一点,我刚刚验证了声明解包的工作方式符合 c3p0-0.9.5.2 中的规定。我很抱歉这是 Scala REPL 代码而不是 Java。这就是方便。但在 JVM 级别的底层操作是相同的。

[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import com.mchange.v2.c3p0._
import com.mchange.v2.c3p0._

scala> val cpds = new ComboPooledDataSource()
Jul 08, 2016 1:44:43 PM com.mchange.v2.log.MLog 
INFO: MLog clients using java 1.4+ standard logging.
Jul 08, 2016 1:44:43 PM com.mchange.v2.c3p0.C3P0Registry 
INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
cpds: com.mchange.v2.c3p0.ComboPooledDataSource = com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> 2u6yea9h1khti8o11c1oj2|2407f8d8, dataSourceName -> 2u6yea9h1khti8o11c1oj2|2407f8d8 ]

scala> cpds.setUser("root")

scala> cpds.setJdbcUrl("jdbc:mysql://localhost/test?useSSL=false&serverTimezone=UTC")

scala> val conn = cpds.getConnection()
Jul 08, 2016 1:45:03 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource 
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 2u6yea9h1khti8o11c1oj2|2407f8d8, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, extensions -> {timezone=HKT}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 2u6yea9h1khti8o11c1oj2|2407f8d8, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost/test?useSSL=false&serverTimezone=UTC, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {user=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
conn: java.sql.Connection = com.mchange.v2.c3p0.impl.NewProxyConnection@6044a933 [wrapping: com.mysql.cj.jdbc.ConnectionImpl@4318f07b]

scala> val proxyStatement = conn.createStatement()
proxyStatement: java.sql.Statement = com.mchange.v2.c3p0.impl.NewProxyStatement@57b482ea [wrapping: com.mysql.cj.jdbc.StatementImpl@b120337]

scala> val nativeStmt = proxyStatement.unwrap(classOf[com.mysql.cj.jdbc.StatementImpl])
nativeStmt: com.mysql.cj.jdbc.StatementImpl = com.mysql.cj.jdbc.StatementImpl@b120337

我正在使用 MySQL Connector/J 版本 6.0.3,它可能比您的更新(因此 Statement 类名称不同)。您可以以更中立的方式打开 Statement:

scala> val nativeStmt2 = proxyStatement.unwrap(classOf[java.sql.Statement])
nativeStmt2: java.sql.Statement = com.mysql.cj.jdbc.StatementImpl@b120337

请注意,Scala 的 classOf[java.sql.Statement] 等价于 Java 的类字面量 java.sql.Statement.class

【讨论】:

  • 感谢您这么快回复。在我的 OSGi 包的类路径中,我只有 hibernate-c3p0-3.6.5.Final.jar 和现在 c3p0-0.9.5.2.jar。问题仍然存在。由于我使用分层框架,它不会在日志中打印,但我在我的服务器的所有捆绑包 OSGi 的 MANIFEST.MF 中搜索,这些是唯一的。我也使用 mysql-connector-java-5.1.37
    可能是hibernate-c3p0 jar太旧了?
  • 顺便看了一下com.mchange.v2.c3p0.impl.NewProxyStatement的源码,发现没有unwrap函数,正常吗?
  • 它就在那里,请参阅line 1402。请注意,这是一个代码生成的文件。你必须构建它,你不会直接找到源代码。 (不过,它被捆绑在 Maven src jar 中。)
  • 你是对的。在旧版本中,通过另一个包导入了另一个 c3p0 包。排除它,它就像一个魅力。非常感谢
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2017-05-09
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多