首页 > 解决方案 > Apache Ignite 是否支持分布式事务

问题描述

我学习了 apache Ignite 的例子。我只想点燃来帮助解决分布式事务。例如。我的账户在DB A,我老婆的账户在DB B。我想给老婆转账。所以这样的交易:

           IgniteTransactions transactions = ignite.transactions();
           p1.setSalary(500);
           p2_1.setSalary(1500);
           Transaction tx = transactions.txStart(TransactionConcurrency.PESSIMISTIC,TransactionIsolation.SERIALIZABLE);
        
         try {
           cache.put(1L, p1);
           
           cache2.put(1L,p2_1);
           
           tx.commit();
         }catch(Exception e) {
             tx.rollback();
         }

但是 cacheStore 是这样的:

    public void write(Entry<? extends Long, ? extends Person> entry) throws CacheWriterException {
      System.out.println(" +++++++++++  single wirte");
       Long key = entry.getKey();
        Person val = entry.getValue();

        System.out.println(">>> Store write [key=" + key + ", val=" + val + ']');

        try {
            Connection conn = dataSource.getConnection();

            int updated;

            // Try update first. If it does not work, then try insert.
            // Some databases would allow these to be done in one 'upsert' operation.
            try (PreparedStatement st = conn.prepareStatement(
                "update PERSON set orgId = ?, name = ?, salary=?  where id = ?")) {
                st.setLong(1, val.getOrgId());
                st.setString(2, val.getName());
                st.setLong(3, val.getSalary());
                st.setLong(4, val.getId());

                updated = st.executeUpdate();
            }

            // If update failed, try to insert.
            if (updated == 0) {
                try (PreparedStatement st = conn.prepareStatement(
                    "insert into PERSON (id, orgId,name, salary) values (?, ?, ?,?)")) {
                    st.setLong(1, val.getId());
                    st.setLong(2, val.getOrgId());
                    st.setString(3, val.getName());
                    st.setLong(4, val.getSalary());
                    st.executeUpdate();
                }
            }
        }
        catch (SQLException e) {
            throw new CacheWriterException("Failed to write object [key=" + key + ", val=" + val + ']', e);
        }
    
    
}

当第一部分提交工资更新时,第二部分失败。第一部分不能回滚。

如何同时提交或回滚它们?ignite 能保证这一点,还是你自己做?

ps:为什么ignite说:它加速交易?似乎它只加速查询,而不是删除或更新操作。因为它在软事务内存发生时同时访问数据库。

有人能弄清楚吗?我不明白点燃的原理。

标签: ignite

解决方案


Apache Ignite 期望缓存存储不会失败。在您的情况下, upsert 非常脆弱并且会失败。

至少,事务操作意味着事务缓存存储。COMMIT;只有在被告知时,您才需要观察缓存存储中的事务。


推荐阅读