0

从我在SyncResolver javadoc 中读到的内容:

应用程序可以调用 SyncResolver 方法 getConflictValue 来检索数据源中导致冲突的值,因为 SyncResolver 对象中的值是来自数据源的冲突值。

所以我正在重现这种情况,但由于某种原因,该方法返回 null 就像根本没有冲突一样。

表定义:

MariaDB [testdb]> SHOW COLUMNS FROM COFFEES;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| COF_NAME | varchar(32)   | NO   | PRI | NULL    |       |
| SUP_ID   | int(11)       | NO   | MUL | NULL    |       |
| PRICE    | decimal(10,2) | NO   |     | NULL    |       |
| SALES    | int(11)       | NO   |     | NULL    |       |
| TOTAL    | int(11)       | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+

表格内容:

MariaDB [testdb]> SELECT * FROM COFFEES;
+--------------------+--------+-------+-------+-------+
| COF_NAME           | SUP_ID | PRICE | SALES | TOTAL |
+--------------------+--------+-------+-------+-------+
| Amaretto           |     49 | 15.61 |     0 |    10 |
| Amaretto_decaf     |     49 | 17.18 |     0 |    20 |
| Colombian          |    101 | 15.61 |   175 |   525 |
| Colombian_Decaf    |    101 | 17.56 |   155 |   465 |
| Espresso           |    150 | 19.51 |    60 |   180 |
| French_Roast       |     49 | 17.56 |   150 |   450 |
| French_Roast_Decaf |     49 | 19.51 |    90 |   270 |
| Hazelnut           |     49 | 15.61 |     0 |     0 |
| Hazelnut_decaf     |     49 | 17.18 |     0 |     0 |
| Kona               |    150 | 17.18 |     0 |     0 |
+--------------------+--------+-------+-------+-------+

测试它的Java代码:

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.spi.SyncProviderException;
import javax.sql.rowset.spi.SyncResolver;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class MinimalExample {

    private static final String USER_NAME = "ewa";
    private static final String URL = "jdbc:mariadb://localhost:3306/testdb";
    private static final int VALUE_TO_SET = 10;
    private static final int COLUMN_INDEX = 5;

    public static void main(String[] args) throws SQLException, IOException {
        var rowSet = createRowSet();
        rowSet.setCommand("SELECT * FROM COFFEES");
        rowSet.execute();
        updateRowSet(rowSet);
    }

    private static void updateRowSet(CachedRowSet rowSet) throws SQLException {
        rowSet.beforeFirst();
        rowSet.next();
        rowSet.updateInt(COLUMN_INDEX, VALUE_TO_SET);
        rowSet.updateRow();

        try (var connection = prepareConnection()) {
            rowSet.acceptChanges(connection);
        } catch (SyncProviderException exception) {
            System.out.println("Conflict detected.");
            solveConflicts(exception.getSyncResolver());
        }
    }

    private static void solveConflicts(SyncResolver resolver) throws SQLException {
        while (resolver.nextConflict()) {
            System.out.println("Conflict on " + resolver.getRow() + " row.");
            System.out.println(resolver.getConflictValue(COLUMN_INDEX));
        }
    }

    private static Connection prepareConnection() throws SQLException {
        var properties = new Properties();
        properties.put("user", USER_NAME);
        var connection = DriverManager.getConnection(URL, properties);
        connection.setAutoCommit(false);
        return connection;
    }

    private static CachedRowSet createRowSet() throws SQLException {
        var factory = RowSetProvider.newFactory();
        var rowSet = factory.createCachedRowSet();
        rowSet.setUsername(USER_NAME);
        rowSet.setUrl(URL);
        return rowSet;
    }

}

我在 updateRowSet 方法的开头设置断点,并从程序外部(终端)运行以下语句:

UPDATE COFFEES SET TOTAL = 60 WHERE COF_NAME = 'Amaretto';

60每次都在更改号码以引起冲突。

我正在使用 Java 11 和 MariaDB 2.53。

4

0 回答 0