- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用带有普通 jdbc 的 spring。并且有多个线程执行各种数据库操作。例如检查表中是否不存在值然后将其插入。问题是我遇到了异常>> 超出锁定等待超时;尝试重新启动事务 我已经检查了 mysql SHOW ENGINE INNODB STATUS\G;它给出了 desc
------------
TRANSACTIONS
------------
Trx id counter D467
Purge done for trx's n:o < D459 undo n:o < 0
History list length 1050
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 64, OS thread handle 0xa6a0ab40, query id 9370 localhost root
SHOW ENGINE INNODB STATUS
---TRANSACTION D466, ACTIVE 22 sec
5 lock struct(s), heap size 320, 2 row lock(s), undo log entries 2
MySQL thread id 74, OS thread handle 0xa6c9eb40, query id 9369 localhost 127.0.0.1 root
Trx read view will not see trx with id >= D467, sees < D457
---TRANSACTION D464, ACTIVE 24 sec
5 lock struct(s), heap size 320, 2 row lock(s), undo log entries 2
MySQL thread id 70, OS thread handle 0xa6aceb40, query id 9354 localhost 127.0.0.1 root
Trx read view will not see trx with id >= D465, sees < D457
---TRANSACTION D462, ACTIVE 28 sec
2 lock struct(s), heap size 320, 1 row lock(s)
MySQL thread id 73, OS thread handle 0xa6c3cb40, query id 9347 localhost 127.0.0.1 root
Trx read view will not see trx with id >= D463, sees < D457
---TRANSACTION D45A, ACTIVE 87 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 34 lock struct(s), heap size 2496, 81 row lock(s), undo log entries 151
MySQL thread id 72, OS thread handle 0xa6a3bb40, query id 9054 localhost 127.0.0.1 root update
Insert into TABLE_1 ( ID,NAME,STATUS)values (44545,'temp','disable')
Trx read view will not see trx with id >= D45B, sees < D450
------- TRX HAS BEEN WAITING 26 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 159967 n bits 312 index `PRIMARY` of table `test2`.`TABLE_1` trx id D45A lock mode S locks rec but not gap waiting
Record lock, heap no 67 PHYSICAL RECORD: n_fields 12; compact format; info bits 0
0: len 4; hex 80016687; asc f ;;
1: len 6; hex 00000000d457; asc W;;
2: len 7; hex 2f0001dda310ee; asc / ;;
3: len 10; hex 4d6f68642e20417a697a; asc Mohd. Aziz;;
4: SQL NULL;
5: SQL NULL;
6: len 1; hex 4d; asc M;;
7: SQL NULL;
8: len 1; hex 30; asc 0;;
9: len 10; hex 496e646976696475616c; asc Individual;;
10: len 6; hex 414354495645; asc ACTIVE;;
11: len 4; hex 53a90e01; asc S ;;
------------------
---TRANSACTION D457, ACTIVE 130 sec
77 lock struct(s), heap size 5504, 141 row lock(s), undo log entries 287
MySQL thread id 71, OS thread handle 0xa6affb40, query id 9001 localhost 127.0.0.1 root
Trx read view will not see trx with id >= D458, sees < D42E
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
2386 OS file reads, 1108 OS file writes, 155 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 884, seg size 886, 12 merges
merged operations:
insert 102, delete mark 6, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 553253, node heap has 18 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
我只是从我的研究中了解到 mysql INNODB 使用行级锁并且删除它需要索引锁(独占)和间隙锁作为共享。由于某种原因,它不会发布它。然后对于同一个表中的其他一些插入,它需要那个锁.. 我得到了这个异常(exception)。谁能建议我如何解决这个问题? 其他需要的详细信息>>
datasource.xml
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:mysql://localhost:3306/test2" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="removeAbandoned" value="true" />
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>
<!-- Enable Annotation based Declarative Transaction Management -->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
这就是我在 spring 事务中使用普通 jdbc 的方式 >>
@Repository
@Transactional(rollbackFor=Exception.class)
public class StudentDaoImpl implements StudentDao {
private static final Logger log = Logger
.getLogger(StudentDaoImpl.class);
@Autowired
private DataSource dataSource;
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
String sql = "Select count(*) from SOMETABLE where ID= ?";
Connection conn = null;
try {
conn = DataSourceUtils.getConnection(dataSource);
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
log.error(e);
throw new Exception(e);
} finally {
close(stmt, rs);
DataSourceUtils.releaseConnection(conn, dataSource);
}
return count;
}
最佳答案
您的问题出在您的数据库操作上,与您访问数据库所使用的技术无关。
出现数据库死锁的唯一原因是在某些事务/操作中,您以相反的顺序锁定资源。
例如:
交易 1:
UPDATE table1 SET col=val WHERE id=1
DELETE FROM table2 WHERE id=2
交易 2:
UPDATE table2 SET col=val WHERE id=2
UPDATE table1 SET col=val WHERE id=1
第一个事务获取对 table1,row 1 的锁定。同时,第二个事务获得了对 table2,第 2 行 的锁定。
然后,第一个事务想要从 table2 中获取第 2 行,但是不能,因为第二个事务已经有了它。因此,tr1 必须等待。
同样,tr2 也必须等待,因为它想从表 1 中获取 第 1 行,但 tr1 持有它。所以,他们在等待对方,只有超时才能释放(并回滚)其中一个事务(另一个可以运行,如果它有足够的剩余时间)。
解决方案:
您为您的表赋予优先级,并始终按该顺序使用它们(例如 table1、table2 )。如果您的逻辑要求您首先需要 table2 中的一些数据,您可以考虑颠倒顺序或锁定 table1 中肯定会覆盖最终操作中涉及的行的最小可能行集(可能是您必须锁定 table1 中的所有行,或者改为放置表级锁)。
是的,所有操作,包括 GUI 查询、后台进程、报告等,都必须使用相同的顺序。
关于java - 超过锁定等待超时;尝试用 spring 和 Mysql 重启事务 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379132/
我正在使用 PostgREST 将数据库实体暴露给使用这些实体的 Springboot 应用。 我的数据库中有两个实体,分别是 Person 和 City。 我想同时保存 Person 实体和 Cit
1、事务的定义 Redis的事务提供了一种“将多个命令打包, 然后一次性、按顺序地执行”的机制。 redis事务的主要作用就是串联多个命令防止别的命令插队。 但是,事务并不具有传统
SQLite 事务(Transaction) 事务(Transaction)是一个对数据库执行工作单元。事务(Transaction)是以逻辑顺序完成的工作单位或序列,可以是由用户手动操作完成,也可
事务是顺序组操作。 它们作为单个单元运行,并且直到组中的所有操作都成功执行时才终止。 组中的单个故障会导致整个事务失败,并导致对数据库没有影响。 事务符合ACID(原子性,一致性,隔离和耐久性)
我希望将 SqlKata 用于一个项目。但是,项目标准的一部分是查询应该能够作为事务执行。有没有一种方法可以使用 MSSQL 事务执行一个查询或多个查询? 非常感谢。 最佳答案 SQLKata 使用
我只是以多线程方式测试 PetaPoco 事务... 我有一个简单的测试用例: -- 简单的值对象称之为 MediaDevice -- 插入一条记录,更新1000次 void TransactionT
我正在尝试从 Excel VBA 向 SQL 中插入一些数据。 SQL 命令是在 VBA 脚本的过程中构建的,包括使用一些 SQL 变量。 我试图了解事务在 VBA 中是如何工作的,以及它们是否可以处
情况如下: 一个大型生产客户端/服务器系统,其中一个中央数据库表具有某个列,该列的默认值是 NULL,但现在默认值是 0。但是在该更改之前创建的所有行当然仍然具有 null 值,这会在该系统中生成许多
数据库事务是一个熟悉的概念。 try { ... .. updateDB() .. ... commit(); } catch error { rollback(); }
我想了解使用传播支持进行 Spring 交易的用途。 java 文档提到如果具有 @Transactional(propagation = Propagation.SUPPORTS) 的方法从支持该事
我需要获取 hibernate 的事务 ID。对于每笔交易,此 ID 必须是唯一的。我尝试使用 session.getTransaction().hashCode(),但我相信这个值不是唯一的。 最佳
我从 firebase 收到以下消息:runTransactionBlock:启用持久性时检测到的使用情况。请注意,事务不会在应用重新启动后保留。 那么应用程序重新启动后到底会发生什么?由于主数据库的
我需要在 jdbc 中执行选择、更新、插入查询的序列。 这是我的代码: public String editRequest(){ connection = DatabaseUtil.getServi
Java 是否提供了一种智能“聚合”事务的方法?如果我有多个异构数据存储库,我想保持同步(即用于数据的 Postgres、用于图表的 Neo4j 以及用于索引的 Lucene),是否有一个范例仅允许
我对标题中的主题有几个问题。首先,假设我们使用 JDBC,并且有 2 个事务 T1 和 T2。在 T1 中,我们在一个特定的行上执行 select 语句。然后我们对该行执行更新。在事务 T2 中,我们
我有一个 Python CGI 处理支付交易。当用户提交表单时,CGI 被调用。提交后,CGI 需要一段时间才能执行信用卡交易。在此期间,用户可能会按下 ESC 或刷新按钮。这样做不会“杀死”CGI,
我有一个代码,类似这样 def many_objects_saving(list_of_objects): for some_object in list_of_objects:
我有一个包含 100,000 条记录的表。我正在考虑使用事务来更新数据。将有一个查询将一列更新为零,并且大约有 5000 个更新,每个更新将更新一条记录。 这些大型事务对内存有何影响?事务运行时选择数
有没有办法在一个命令中执行 SQL 事务?例如 mysql_query(" START TRANSACTION; INSERT INTO table1 ....etc; INSERT INTO tab
真心希望能帮到你! 我使用以下函数在 PHP/MySql 应用程序中发送消息: public function sendMail($sender_id, $recipient_id, $subject
我是一名优秀的程序员,十分优秀!