gpt4 book ai didi

java - 没有提交 Ignite CacheStoreAdapter

转载 作者:行者123 更新时间:2023-11-30 02:20:59 24 4
gpt4 key购买 nike

CacheStoreAdapter 错误。当调用其方法 sessionEnd 时,CacheStoreSessionResource 的连接始终处于关闭状态。没有任何异常(exception)。但实际上事务未提交,数据库中也没有任何更改。我的代码很简单。一切都是按照原来的Ignite例子来完成的。

客户端

IgniteCache<String, String> typeCache = ignite.getOrCreateCache("typeCache");
try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC,
TransactionIsolation.REPEATABLE_READ)) {
typeCache.put("code", "name");
tx.commit();
}
catch (Exception e) {
log.error("ERROR. Put Type: " + type, e);
}

服务器:

public class CacheJdbcTypeStore extends CacheStoreAdapter<String, String> {

@CacheStoreSessionResource
private CacheStoreSession ses;

@Override
public void write(Cache.Entry<? extends String, ? extends String> entry) {
String key = entry.getKey();
String val = entry.getValue();

try (Connection conn = connection(ses)) {
try (PreparedStatement st = conn.prepareStatement(
"insert into t_type (code, name) values (?, ?)")) {
st.setString(1, key);
st.setString(2, val);
st.executeUpdate();
}
}
catch (Exception e) {
throw new CacheWriterException("Failed to write Type " + val, e);
}
}

@Override
public void sessionEnd(boolean commit) {
try (Connection conn = ses.attachment()) {
if (conn != null && !conn.isClosed() && ses.isWithinTransaction()) {
if (commit)
conn.commit();
else
conn.rollback();
}
}
catch (SQLException e) {
throw new CacheWriterException("Failed to end store session of Type cache", e);
}
}

private Connection connection(CacheStoreSession ses) throws Exception {
if (ses.isWithinTransaction()) {
Connection conn = ses.attachment();
/************************************/
/* Here conn always is closed. WHY???? */
/* As result transaction is never commited !!!! */
/************************************/
if (conn == null || conn.isClosed()) {
conn = openConnection(false);
ses.attach(conn);
}
return conn;
}
else {
return openConnection(true);
}
}

// Opens JDBC connection.
private Connection openConnection(boolean autocommit) throws Exception {
Connection conn = DriverManager.getConnection(url);
conn.setAutoCommit(autocommit);
return conn;
}

@Override
public String load(final String key) {
return null;
}

@Override
public void delete(Object key) {
}

}

配置

<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="typeCache" />
<property name="cacheMode" value="PARTITIONED" />
<property name="atomicityMode" value="TRANSACTIONAL" />
<property name="backups" value="1" />
<property name="cacheStoreFactory">
<bean class="javax.cache.configuration.FactoryBuilder"
factory-method="factoryOf">
<constructor-arg
value="ru.raiffeisen.cache.store.jdbc.CacheJdbcTypeStore" />
</bean>
</property>
<property name="readThrough" value="true" />
<property name="writeThrough" value="true" />
</bean>
</list>
</property>

最佳答案

您使用 try-with-resource 进行连接,因此每次离开此 block 时,连接都会关闭。

try (Connection conn = ses.attachment()) {}

我认为您检查了此实现:https://apacheignite.readme.io/docs/3rd-party-store#section-cachestore-example但是,正如您所看到的,它提到这不是交易。 请查看this缓存存储实现作为事务缓存存储的示例

此外,要查看 conn 变量中的非空值,请尝试在事务内添加多个插入。

关于java - 没有提交 Ignite CacheStoreAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46830349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com