gpt4 book ai didi

java - Hibernate 中的最大并发事务数是否有限制?

转载 作者:行者123 更新时间:2023-12-01 05:14:25 25 4
gpt4 key购买 nike

我有一个客户端向使用 Hibernate 的服务层发送 3 个请求。

每个请求都会导致 Hibernate 开始一个事务 (session.beginTransaction())。

我发现,有时,一个事务(来自 2 个以上正在运行的并发事务)会失败,并显示createQuery is not valid without active transaction

这是我使用的 Hibernate 配置(在 Tomcat 6.0.x 和 OC4j 10.1.3.4 中运行):

<property name="hibernate.connection.pool_size">5</property>
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>

<!-- auto commit -->
<!-- <property name="connection.autocommit">true</property> -->

<!-- configuration pool via c3p0 -->
<property name="c3p0.idleConnectionTestPeriod">1000</property>
<property name="c3p0.initialPoolSize">5</property>
<property name="c3p0.maxPoolSize">10</property>
<property name="c3p0.maxIdleTime">1</property>
<property name="c3p0.maxStatements">30</property>
<property name="c3p0.minPoolSize">1</property>

<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="cache.use_second_level_cache">true</property>

编辑:我使用以下代理来管理所有事务:

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


/**
* http://stackoverflow.com/questions/2587702
*
* @author mohammad_abdullah
*/
public class ServiceProxy implements InvocationHandler {

private Object object;
private Logger logger = Logger.getLogger(this.getClass().getSimpleName());
private static final String SESSION_FIELD = "session";

public static final Map<Long, Transaction> ACTIVE_TRANSACTIONS = new HashMap<Long, Transaction>();

private ServiceProxy(Object object) {
this.object = object;
}

public static Object newInstance(Object object) {
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), new ServiceProxy(object));
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

Object result = null;
Session session = null;
boolean joined = false;
try {
if (Modifier.isPublic(method.getModifiers())) {

session = HibernateUtil.getSessionFactory().getCurrentSession();

Field sessionField = object.getClass().getSuperclass().getDeclaredField(SESSION_FIELD);
if (sessionField == null)
throw new UPSAdminException("Service Implementation should have field named: \"" + SESSION_FIELD + "\".");
sessionField.setAccessible(true);
sessionField.set(object, session);

if (session.getTransaction().isActive()) {
joined = true;
logger.info("Using Already Active transaction" + " Method: " + method.getName() + " Thread: "
+ Thread.currentThread().getId());
ACTIVE_TRANSACTIONS.put(Thread.currentThread().getId(), session.getTransaction());
} else {
logger.info("Transaction Began" + " Method: " + method.getName() + " Thread: " + Thread.currentThread().getId());
Transaction newTnx = session.beginTransaction();
ACTIVE_TRANSACTIONS.put(Thread.currentThread().getId(), newTnx);
}
result = method.invoke(object, args);

if (!joined) {
ACTIVE_TRANSACTIONS.get(Thread.currentThread().getId()).commit();
ACTIVE_TRANSACTIONS.remove(Thread.currentThread().getId());
logger.info("Transaction Commited" + " Method: " + method.getName() + " Thread: " + Thread.currentThread().getId());
}
} else {
result = method.invoke(object, args);
}

return result;

} catch (InvocationTargetException _ex) {
Throwable cause = _ex.getCause();
logger.severe("Caller Exception: " + cause + " Method: " + method.getName() + " Thread: " + Thread.currentThread().getId());

if (!joined && session != null && session.getTransaction().isActive()) {
ACTIVE_TRANSACTIONS.get(Thread.currentThread().getId()).rollback();
ACTIVE_TRANSACTIONS.remove(Thread.currentThread().getId());
}

if (cause instanceof HibernateException) {
logger.severe("Hibernate Error. Rollbacked Back. Method: " + method.getName() + " Thread: "
+ Thread.currentThread().getId());
throw new DBException(cause.getCause().getMessage());

} else if (cause instanceof SetRollbackException) {
logger.severe("Transaction marked for Rollback. Rollbacked Back. Method: " + method.getName() + " Thread: "
+ Thread.currentThread().getId());
return result;

} else {
logger.severe("Error in Business Method : " + method + ". Rollbacked Back." + " Thread: " + Thread.currentThread().getId());
throw cause;
}
} catch (Exception ex) {
logger.severe("Error in Proxy code :" + ex + " Method :" + method + " Thread: " + Thread.currentThread().getId());

if (!joined && session != null && session.getTransaction().isActive()) {
ACTIVE_TRANSACTIONS.get(Thread.currentThread().getId()).rollback();
ACTIVE_TRANSACTIONS.remove(Thread.currentThread().getId());
}

if (ex instanceof HibernateException)
throw new DBException(ex.getCause().getMessage());

throw ex;
}
}
}

最佳答案

由于您已经创建了大小为 5 的连接池,因此对于并发事务来说,它的运行应该不会出现太多问题。 createQuery() 方法更新脏持久性对象,它需要在事务内运行。我认为这就是您错误的原因。

说到事务和连接,每个事务都必须持有一个连接,但由于连接是池化的,如果事务处于等待状态,它会将连接释放回池中。如果并发事务太多而连接数量较少,则会延迟处理。对于长事务,甚至有可能出现死锁等问题......

您可以找到 transaction 的 hibernate API和 Contextual sessions在链接中。

关于java - Hibernate 中的最大并发事务数是否有限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11506159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com