gpt4 book ai didi

hibernate - 尝试锁定中断的原因

转载 作者:行者123 更新时间:2023-12-04 04:32:55 24 4
gpt4 key购买 nike

我的代码表现随机。我使用 Hibernate 作为 ORM,使用 JBoss6 作为应用服务器。我随机收到以下异常。我无法找出原因。

Caused by: java.sql.SQLException: Interrupted attempting lock: org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@5458ca9
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.tryLock(BaseWrapperManagedConnection.java:402)
at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:146)
at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:394)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2412) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
... 31 more
12:48:50,276 ERROR [stderr] (pool-6-thread-1) org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: JDBC rollback failed
12:48:50,277 ERROR [stderr] (pool-6-thread-1) at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:679)
12:48:50,277 ERROR [stderr] (pool-6-thread-1) at org.springframework.transaction.support.AbstractPlatformTransactionManager.doRollbackOnCommitException(AbstractPlatformTransactionManager.java:892)
12:48:50,277 ERROR [stderr] (pool-6-thread-1) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:782)
12:48:50,277 ERROR [stderr] (pool-6-thread-1) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
12:48:50,277 ERROR [stderr] (pool-6-thread-1) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at com.verisk.dep.common.aspect.DaoInterceptor.invoke(DaoInterceptor.java:38)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at $Proxy136.userRegistration(Unknown Source)
12:48:50,278 ERROR [stderr] (pool-6-thread-1) at

最佳答案

我遇到了类似的问题。

在我的例子中,我的程序有一个 ThreadPool,其中包含一些使用 java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate 方法以固定速率安排的任务。

在某些给定情况下,当我不得不停止并删除其中一些任务时,我使用了方法 java.util.concurrent.Future.cancel(boolean mayInterruptIfRunning) 并将 mayInterruptIfRunning 设置为 true。

这导致软件抛出“java.sql.SQLException: Interrupted attempting lock”异常。我认为这是因为程序取消了中断线程的任务并且它正在运行数据库操作或类似的东西。

因此,我将 mayInterruptIfRunning 参数更改为 false,并强制程序在任务尚未完成和取消时等待。

这里是一个解决方案的例子:

List<ScheduledFuture> tasks = new ArrayList<ScheduledFuture>();
ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(5);

tasks.add(threadPool.scheduleAtFixedRate(yourRunnableTask, 0L, 60000L, TimeUnit.MILLISECONDS));

...

for(int i=0; i<tasks.size(); i++)
{
ScheduledFuture task = tasks.get(i);
task.cancel(false);

int retrys = 0;

while(!task.isCancelled() && retrys < 3)
{
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}

retrys++
}

if(task.isCancelled())
{
// This is not actually neccesary (only a good practice)
task.cancel();
threadPool.remove(task);
}
}

关于hibernate - 尝试锁定中断的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281123/

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