Search in sources :

Example 1 with ClientTransaction

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[]
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientTransaction(org.apache.ignite.client.ClientTransaction) ClientTransactionConfiguration(org.apache.ignite.configuration.ClientTransactionConfiguration) ClientCache(org.apache.ignite.client.ClientCache) ClientTransactions(org.apache.ignite.client.ClientTransactions) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with ClientTransaction

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();
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) IgniteException(org.apache.ignite.IgniteException) ClientTransaction(org.apache.ignite.client.ClientTransaction) Ignite(org.apache.ignite.Ignite) ClientException(org.apache.ignite.client.ClientException) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteException(org.apache.ignite.IgniteException) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 3 with ClientTransaction

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;
}
Also used : TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) A(org.apache.ignite.internal.util.typedef.internal.A) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) ClientTransactions(org.apache.ignite.client.ClientTransactions) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientException(org.apache.ignite.client.ClientException) ClientTransaction(org.apache.ignite.client.ClientTransaction) Map(java.util.Map) TRANSACTIONS(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.TRANSACTIONS) ClientTransactionConfiguration(org.apache.ignite.configuration.ClientTransactionConfiguration) ClientException(org.apache.ignite.client.ClientException) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx)

Example 4 with ClientTransaction

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[]
}
Also used : ClientTransaction(org.apache.ignite.client.ClientTransaction) ClientTransactions(org.apache.ignite.client.ClientTransactions)

Example 5 with ClientTransaction

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));
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientTransaction(org.apache.ignite.client.ClientTransaction) BinaryObject(org.apache.ignite.binary.BinaryObject) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration)

Aggregations

ClientTransaction (org.apache.ignite.client.ClientTransaction)7 ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)3 ClientTransactions (org.apache.ignite.client.ClientTransactions)3 IgniteClient (org.apache.ignite.client.IgniteClient)3 ClientException (org.apache.ignite.client.ClientException)2 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)2 ClientTransactionConfiguration (org.apache.ignite.configuration.ClientTransactionConfiguration)2 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)2 TransactionConcurrency (org.apache.ignite.transactions.TransactionConcurrency)2 TransactionIsolation (org.apache.ignite.transactions.TransactionIsolation)2 Test (org.junit.Test)2 Map (java.util.Map)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1