3

我的项目处于起步阶段,我在休眠时遇到了一个大问题。我正在使用带有 c3p0 0.9.1.2 的休眠 4.1.8-Final。

我有以下实体类:

@javax.persistence.Entity
@Table(name = "CUSTOMER")
public class Customer extends Entity {

    @Transient
    private static final long serialVersionUID = 3326144323537452197L;

    private String customerId;
    private String customerName;
    private boolean active;

    public Customer() {
        super();
    }

    @NaturalId
    @Column(name = "CUSTOMER_ID", nullable = false)
    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    @Lob
    @Column(name = "CUSTOMER_NAME", nullable = false)
    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    @Column(name = "ACTIVE", nullable = false)
    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}

如您所见,customerName已设置为Lob. 现在第一次保存操作给了我以下错误:

2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController] java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setCharacterStream(ILjava/io/Reader;J)V
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at java.lang.reflect.Method.invoke(Method.java:601)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at $Proxy240.setCharacterStream(Unknown Source)
2012-12-15 01:37:25,670 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$3$1.doBind(ClobTypeDescriptor.java:83)
2012-12-15 01:37:25,685 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
2012-12-15 01:37:25,685 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
2012-12-15 01:37:25,685 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
2012-12-15 01:37:25,701 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57)
2012-12-15 01:37:25,701 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2747)
2012-12-15 01:37:25,701 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2722)
2012-12-15 01:37:25,701 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.persister.entity.AbstractEntityPersister$4.bindValues(AbstractEntityPersister.java:2929)
2012-12-15 01:37:25,701 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
2012-12-15 01:37:25,716 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2936)
2012-12-15 01:37:25,716 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447)
2012-12-15 01:37:25,716 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
2012-12-15 01:37:25,716 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
2012-12-15 01:37:25,716 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
2012-12-15 01:37:25,732 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)
2012-12-15 01:37:25,732 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)
2012-12-15 01:37:25,732 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)
2012-12-15 01:37:25,732 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
2012-12-15 01:37:25,748 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
2012-12-15 01:37:25,748 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
2012-12-15 01:37:25,748 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
2012-12-15 01:37:25,748 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
2012-12-15 01:37:25,748 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
2012-12-15 01:37:25,763 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
2012-12-15 01:37:25,763 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
2012-12-15 01:37:25,763 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:756)
2012-12-15 01:37:25,763 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.internal.SessionImpl.save(SessionImpl.java:748)
2012-12-15 01:37:25,779 ERROR [com.edfx.adb.web.controller.AdminController]     at org.hibernate.internal.SessionImpl.save(SessionImpl.java:744)
2012-12-15 01:37:25,779 ERROR [com.edfx.adb.web.controller.AdminController]     at com.edfx.adb.dao.base.BaseDao.save(BaseDao.java:21)
2012-12-15 01:37:25,779 ERROR [com.edfx.adb.web.controller.AdminController]     at com.edfx.adb.service.CustomerService.addNewCustomer(CustomerService.java:26)
2012-12-15 01:37:25,779 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2012-12-15 01:37:25,779 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at java.lang.reflect.Method.invoke(Method.java:601)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
2012-12-15 01:37:25,794 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
2012-12-15 01:37:25,810 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
2012-12-15 01:37:25,810 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
2012-12-15 01:37:25,810 ERROR [com.edfx.adb.web.controller.AdminController]     at $Proxy236.addNewCustomer(Unknown Source)
2012-12-15 01:37:25,810 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2012-12-15 01:37:25,810 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2012-12-15 01:37:25,826 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2012-12-15 01:37:25,826 ERROR [com.edfx.adb.web.controller.AdminController]     at java.lang.reflect.Method.invoke(Method.java:601)
2012-12-15 01:37:25,826 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at $Proxy237.addNewCustomer(Unknown Source)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at com.edfx.adb.web.controller.AdminController.saveCustomer(AdminController.java:25)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2012-12-15 01:37:25,841 ERROR [com.edfx.adb.web.controller.AdminController]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2012-12-15 01:37:25,857 ERROR [com.edfx.adb.web.controller.AdminController]     at java.lang.reflect.Method.invoke(Method.java:601)
2012-12-15 01:37:25,857 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
2012-12-15 01:37:25,857 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
2012-12-15 01:37:25,857 ERROR [com.edfx.adb.web.controller.AdminController]     at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
2012-12-15 01:37:25,857 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
2012-12-15 01:37:25,872 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
2012-12-15 01:37:25,872 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
2012-12-15 01:37:25,872 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.component.UICommand.broadcast(UICommand.java:300)
2012-12-15 01:37:25,872 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.component.UIData.broadcast(UIData.java:1093)
2012-12-15 01:37:25,872 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
2012-12-15 01:37:25,888 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
2012-12-15 01:37:25,888 ERROR [com.edfx.adb.web.controller.AdminController]     at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
2012-12-15 01:37:25,888 ERROR [com.edfx.adb.web.controller.AdminController]     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
2012-12-15 01:37:25,904 ERROR [com.edfx.adb.web.controller.AdminController]     at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
2012-12-15 01:37:25,904 ERROR [com.edfx.adb.web.controller.AdminController]     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
2012-12-15 01:37:25,904 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
2012-12-15 01:37:25,904 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
2012-12-15 01:37:25,904 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119)
2012-12-15 01:37:25,919 ERROR [com.edfx.adb.web.controller.AdminController]     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
2012-12-15 01:37:25,919 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
2012-12-15 01:37:25,919 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
2012-12-15 01:37:25,919 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
2012-12-15 01:37:25,935 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
2012-12-15 01:37:25,935 ERROR [com.edfx.adb.web.controller.AdminController]     at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
2012-12-15 01:37:25,935 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
2012-12-15 01:37:25,935 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
2012-12-15 01:37:25,935 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
2012-12-15 01:37:25,950 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
2012-12-15 01:37:25,950 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
2012-12-15 01:37:25,950 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
2012-12-15 01:37:25,950 ERROR [com.edfx.adb.web.controller.AdminController]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
2012-12-15 01:37:25,950 ERROR [com.edfx.adb.web.controller.AdminController]     at java.lang.Thread.run(Thread.java:722)

简而言之,错误消息是:

java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setCharacterStream(ILjava/io/Reader;J)V

通过谷歌搜索这个错误,我发现第一个链接说降级到休眠 4.1.3-Final 可以解决这个问题。这奏效了。但我的目标是使用最新版本的休眠。我读到hibernate没有太多的连接池。导致何时需要连接池;它将从服务器获取该池。

我的问题是,在 JBoss AS 7 中运行的生产环境中,是否有必要使用第三方 c3p0;如果没有,我可以删除 c3p0;或者有这个问题的解决方案?

4

3 回答 3

11

请更新到 c3p0-0.9.2-pre7。它已经成熟——除非出现意外情况,否则下一个版本将是 c3p0-0.9.2 最终版本。hibernate 最近添加了一些 JDBC4 依赖项。c3p0 还没有完整的 JDBC4 支持(它将,但在 0.9.2 最终确定之后),但 c3p0-0.9.2-pre7 支持这些方法来维护休眠依赖。

0.9.2-pre7 是 0.9.1.2 的替代版本。只需删除 c3p0-0.9.1.2.jar 并替换为 BOTH c3p0-0.9.2-pre7.jar 和 mchange-commons-java-0.2.3.2.jar。或者只使用 Maven 中央 [groupId: com.mchange, artifactId: c3p0, version: 0.9.2-pre7],让传递依赖处理其余的事情。请参阅文档的最顶部,http://www.mchange.com/projects/c3p0/以获取下载信息等。

休眠时不需要 c3p0。您可以使用其他连接池。但是虽然我有偏见,但我认为c3p0是一个不错的选择!(我是 c3p0 的作者。)

于 2012-12-14T23:11:28.410 回答
4

较新版本的休眠与0.9.1.1. 我使用hibernate-c3p0:4.3.0.CR1which 明确地依赖于c3p0:0.9.2.1.

可能的原因:

  • 您的项目中可能还有其他一些依赖项使用旧版本的 c3p0 作为传递依赖项
  • 由于较旧的组 id 是c3p0和较新的com.mchange,maven 可能已将它们都添加到类路径中
  • 偶然地,类加载器从而c3p0:c3p0:0.9.1.1不是加载类com.mchange:c3p0:0.9.2.1(尽管 groupId 改变了,包是相同的)

解决方案

c3p0 依赖的 grep

`$mvn dependency:tree |grep -B5 "c3p0"`
[INFO] +- org.hibernate:hibernate-c3p0:jar:4.3.0.CR1:compile
[INFO] |  \- com.mchange:c3p0:jar:0.9.2.1:compile
[INFO] |     \- com.mchange:mchange-commons-java:jar:0.2.3.4:compile
--
[INFO] +- org.quartz-scheduler:quartz:jar:2.2.1:compile
[INFO] |  \- c3p0:c3p0:jar:0.9.1.1:compile

排除老一辈!

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>${quartz.version}</version>
    <exclusions>
        <exclusion>
             <groupId>c3p0</groupId>
             <artifactId>c3p0</artifactId>
             <!-- Bcoz this version is incompatible with hibernate -->
         </exclusion>
    </exclusions>
</dependency>
于 2014-11-13T08:51:49.977 回答
2

休眠核心 4.2.8.Final,休眠 c3p0 4.2.8.Final,c3p0 0.9.2.x。有一个类似的问题:java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setNString(ILjava/lang/String;)V

将 c3p0 版本更改为 0.9.5-pre6 并修复了问题。

于 2013-12-16T11:26:40.780 回答