我学习了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);
}
}
当第一部分承诺工资更新时,第二部分失败。第一部分无法回滚。
如何同时提交或回滚它们?点燃能保证这一点还是您自己做?
ps:为什么点燃说:它加速了交易?似乎它只会加速查询,而不是删除或更新操作。因为它在发生软事务存储时同时访问数据库。
有人能弄清楚吗?我不明白点燃的原理。
答案 0 :(得分:0)
Ignite支持交易。但是有两件事要考虑:
我不确定您在哪里看到过Ignite处理事务的速度更快,但是一般的原则是,通过将所有内容都保留在内存中,Ignite可以比传统数据库快得多。
答案 1 :(得分:0)
Apache Ignite希望缓存存储不会失败。对于您而言,upsert非常脆弱,并且将失败。
至少,事务操作暗含事务缓存存储。您只需要在COMMIT;
的情况下观察缓存存储中的事务即可。