简体   繁体   English

MySQL事务性删除和Java

[英]MySQL Transactional delete and Java

I've got tables like this: 我有这样的表:

Table A:  
`id | name`

Table B:  
`id | A_id | ....`  

A_id is a foreign key to Table A, the Engine is InnoDB

This is the code that fails: 这是失败的代码:

    String[] cleanupQueries = new String[] { "DELETE FROM B WHERE A_id = (SELECT id FROM A WHERE name = 'test')",
                                            "DELETE FROM A WHERE name = 'test'" };

    Connection connection;
    try {
        connection = DriverManager.getConnection(getConnectionString());
        connection.setAutoCommit(false);
    } catch (SQLException e) {
        throw new RuntimeException("Error establishing a database connection!");
    }

    try {
        for(String cleanupQuery : cleanupQueries) {
            PreparedStatement statement = connection.prepareStatement(cleanupQuery);
            statement.executeUpdate(); //FAILS WHEN EXECUTING THE SECOND QUERY
        }
    } catch(SQLException e) {
        throw new RuntimeException("Error while executing the queries in the transactional context!");
    }

    try {
        connection.commit();
    } catch (SQLException e) {
        rollback(connection);
        throw new RuntimeException("Error while comitting!");
    }

The Exception i get is: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails ('DATABASE/TABLE', CONSTRAINT 'FK_B_A' FOREIGN KEY ('FK_A') REFERENCES 'A' ('ID') ON DEL) 我得到的异常是: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails ('DATABASE/TABLE', CONSTRAINT 'FK_B_A' FOREIGN KEY ('FK_A') REFERENCES 'A' ('ID') ON DEL)

The database doesn't let me delete A while there are still B's left, but the first query deleted all B's. 该数据库不允许我删除A,而B仍然剩余,但是第一个查询删除了所有B。 I want to delete all B's and the A they reference only completely. 我只想删除它们仅引用的所有B和A。

I don't want to change the Tables to have cascading deletes. 我不想更改表以进行级联删除。 What shall i do to get the code working? 我应该怎么做才能使代码正常工作?

Cause for error is 错误原因是

The Foreign Key has referenced to the table A id so if you would like to delete the F_Key , first you should delete the Child references values of that foreign keys then only its possible to delete the parent. 外键已引用表A id,因此,如果要删除F_Key,首先应删除该外键的Child引用值,然后才可以删除父键。

Correct me if 'm wrong.. 如果错了,请纠正我。

删除外键约束时,只需添加级联为true。删除原始父项时,子表项会自动删除。

尝试:

"DELETE FROM B WHERE A_id = (SELECT id FROM A WHERE name IN 'test')"

Since the child rows are deleted in the same transaction, the deleted rows are still visible and thus the parent rows could not be deleted. 由于在同一事务中删除了子行,因此删除的行仍然可见,因此无法删除父行。 This may be because of the transaction isolation setting on the connection. 这可能是由于连接上的事务隔离设置所致。 I would try different levels and see which one allows it. 我会尝试不同的级别,看看哪个允许它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM