use of org.apache.ignite.client.ClientTransaction in project ignite by apache.
the class JavaThinClient method transactionConfiguration.
@Test
void transactionConfiguration() {
// tag::transaction-config[]
ClientConfiguration cfg = new ClientConfiguration();
cfg.setAddresses("localhost:10800");
cfg.setTransactionConfiguration(new ClientTransactionConfiguration().setDefaultTxTimeout(10000).setDefaultTxConcurrency(TransactionConcurrency.OPTIMISTIC).setDefaultTxIsolation(TransactionIsolation.REPEATABLE_READ));
IgniteClient client = Ignition.startClient(cfg);
// end::transaction-config[]
ClientCache cache = client.createCache(new ClientCacheConfiguration().setName("test").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
// tag::tx-custom-properties[]
ClientTransactions tx = client.transactions();
try (ClientTransaction t = tx.txStart(TransactionConcurrency.OPTIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
cache.put(1, "new value");
t.commit();
}
// end::tx-custom-properties[]
}
use of org.apache.ignite.client.ClientTransaction in project ignite by apache.
the class TimeoutTest method testClientTimeoutOnOperation.
/**
* Test client timeout on operation.
*/
@Test
@SuppressWarnings("ThrowableNotThrown")
public void testClientTimeoutOnOperation() throws Exception {
try (Ignite ignite = startGrid(0)) {
try (IgniteClient client = startClient(0)) {
ClientCache<Object, Object> cache = client.getOrCreateCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
doSleep(TIMEOUT * 2);
// Should not fail if connection is idle.
cache.put(0, 0);
CyclicBarrier barrier = new CyclicBarrier(2);
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(0, 0);
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new IgniteException(e);
}
});
// Wait for the key locked.
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
long ts0 = System.currentTimeMillis();
try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
try {
GridTestUtils.assertThrowsWithCause(() -> cache.put(0, 0), ClientException.class);
} finally {
// To unlock another thread.
barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
}
}
long ts1 = System.currentTimeMillis();
assertTrue("Unexpected timeout [ts0=" + ts0 + ", ts1=" + ts1 + ']', ts1 - ts0 >= TIMEOUT && ts1 - ts0 < TIMEOUT * 2);
fut.get();
}
}
}
use of org.apache.ignite.client.ClientTransaction in project ignite by apache.
the class TcpClientTransactions method txStart0.
/**
* @param concurrency Concurrency.
* @param isolation Isolation.
* @param timeout Timeout.
*/
private ClientTransaction txStart0(TransactionConcurrency concurrency, TransactionIsolation isolation, Long timeout, String lb) {
TcpClientTransaction tx0 = tx();
if (tx0 != null)
throw new ClientException("A transaction has already been started by the current thread.");
tx0 = ch.service(ClientOperation.TX_START, req -> {
ProtocolContext protocolCtx = req.clientChannel().protocolCtx();
if (!protocolCtx.isFeatureSupported(TRANSACTIONS)) {
throw new ClientProtocolError(String.format("Transactions are not supported by the server's " + "protocol version %s, required version %s", protocolCtx.version(), TRANSACTIONS.verIntroduced()));
}
try (BinaryRawWriterEx writer = new BinaryWriterExImpl(marsh.context(), req.out(), null, null)) {
writer.writeByte((byte) (concurrency == null ? txCfg.getDefaultTxConcurrency() : concurrency).ordinal());
writer.writeByte((byte) (isolation == null ? txCfg.getDefaultTxIsolation() : isolation).ordinal());
writer.writeLong(timeout == null ? txCfg.getDefaultTxTimeout() : timeout);
writer.writeString(lb);
}
}, res -> new TcpClientTransaction(res.in().readInt(), res.clientChannel()));
threadLocTxUid.set(tx0.txUid);
txMap.put(tx0.txUid, tx0);
return tx0;
}
use of org.apache.ignite.client.ClientTransaction in project ignite by apache.
the class JavaThinClient method tx.
void tx(IgniteClient client) {
// tag::tx[]
ClientCache<Integer, String> cache = client.cache("my_transactional_cache");
ClientTransactions tx = client.transactions();
try (ClientTransaction t = tx.txStart()) {
cache.put(1, "new value");
t.commit();
}
// end::tx[]
}
use of org.apache.ignite.client.ClientTransaction in project ignite by apache.
the class JavaThinCompatibilityTest method testTransactions.
/**
*/
private void testTransactions() throws Exception {
X.println(">>>> Testing transactions");
try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
ClientCache<Object, Object> cache = client.getOrCreateCache(new ClientCacheConfiguration().setName("testTransactions").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
try (ClientTransaction tx = client.transactions().txStart()) {
cache.put(1, 1);
cache.put(2, 2);
tx.commit();
}
assertEquals(1, cache.get(1));
assertEquals(2, cache.get(2));
}
}
Aggregations