休眠状态:c3p0在NewProxyConnection.commit()上引发NullPointerException [英] Hibernate: c3p0 throws NullPointerException at NewProxyConnection.commit()

查看:217
本文介绍了休眠状态:c3p0在NewProxyConnection.commit()上引发NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在4.3.1.Final版本中同时使用了Hibernate和C3P0。 MySQL 5.6.14(所有表均使用InnoDB)。

I am using both Hibernate and C3P0 in version 4.3.1.Final. MySQL 5.6.14 (InnoDB for all tables).

hibernate.cfg.xml中的C3P0设置为:

C3P0 settings in hibernate.cfg.xml are:

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
<property name="hibernate.c3p0.minPoolSize">3</property>
<property name="hibernate.c3p0.maxPoolSize">100</property>

例如,当我进行交易时:

When I commit a transaction for example:

public void editCategory(int id, String title) {
    Transaction tx = getTransaction();
    Query q = session.createQuery("FROM Category WHERE id = :id");
    q.setInteger("id", id);
    Category cat = (Category) q.uniqueResult();
    cat.setTitle(title);
    tx.commit();
}
private Transaction getTransaction() {
    Transaction tx = session.getTransaction();
    if (!tx.isActive()) {
        tx.begin();
    }
    return tx;
}

大约5次尝试中有3次由于以下原因导致提交失败:

In approximately 3 of 5 attempts commit fails because of:

SEVERE: Servlet.service() for servlet [appserver.services.ApplicationConfig] in context with path [/AppServer] threw exception [org.hibernate.TransactionException: commit failed] with root cause
java.lang.NullPointerException
    at com.mchange.v2.c3p0.impl.NewProxyConnection.commit(NewProxyConnection.java:1284)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:112)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:180)
    at appserver.dao.CategoryDao.createCategory(CategoryDao.java:60)
    at appserver.services.CategoryResource.createCategory(CategoryResource.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
    ... stacktrace continues

我缺少C3P0设置吗?感谢帮助!

Is there something with C3P0 settings that I'm missing? Thanks for help!

推荐答案

您的应用服务器报告的是根本原因的根本原因,也就是说,它正在跳过将报告以下原因的异常:更多信息,您不能在封闭的Connection上操作!!!

Your app server is reporting the root cause of the root cause, that is it is skipping an Exception that would have reported the more informative message, "You can't operate on a closed Connection!!!"

您的应用程序中存在竞争条件。您摘录的方法中引用的对象会话有时很可能在查询和提交之间处于close()状态。 (休眠会话封装了JDBC连接。)

You have a race condition in your application. The Object 'session' referenced in the methods you excerpt is very likely sometimes close()ed between the query and the commit. (The hibernate Session wraps the JDBC Connection.)

您需要了解会话的生命周期,并确保永远不会发生这种情况。最简单地说,您可能根本避免缓存会话。您将在需要时获取它们,并在使用后即commit()完成交易后立即关闭它们(可靠地,通过finally块或try-with-resources构造)。

You need to understand the lifecycle of your Sessions, and ensure that this never happens. Most simply, you might avoid caching the Sessions at all. You would acquire them when needed, and close them immediately after use, that is after you've commit()ed your transaction (reliably, via a finally block or try-with-resources construct).

这篇关于休眠状态:c3p0在NewProxyConnection.commit()上引发NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆