Search in sources :

Example 1 with Transaction

use of org.apache.ignite.tx.Transaction in project ignite-3 by apache.

the class ItThinClientTransactionsTest method testAccessLockedKeyTimesOut.

@Test
void testAccessLockedKeyTimesOut() {
    KeyValueView<Integer, String> kvView = kvView();
    Transaction tx = client().transactions().begin();
    kvView.put(tx, -100, "1");
    IgniteClientException ex = assertThrows(IgniteClientException.class, () -> kvView.get(null, -100));
    assertThat(ex.getMessage(), containsString("TimeoutException"));
    tx.rollback();
}
Also used : IgniteClientException(org.apache.ignite.client.IgniteClientException) Transaction(org.apache.ignite.tx.Transaction) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 2 with Transaction

use of org.apache.ignite.tx.Transaction in project ignite-3 by apache.

the class ItThinClientTransactionsTest method testRecordViewBinaryOperations.

@Test
void testRecordViewBinaryOperations() {
    RecordView<Tuple> recordView = table().recordView();
    Tuple key = key(1);
    recordView.upsert(null, kv(1, "1"));
    Transaction tx = client().transactions().begin();
    recordView.upsert(tx, kv(1, "22"));
    assertFalse(recordView.deleteExact(tx, kv(1, "1")));
    assertFalse(recordView.insert(tx, kv(1, "111")));
    assertEquals(kv(1, "22"), recordView.get(tx, key));
    assertEquals(kv(1, "22"), recordView.getAndUpsert(tx, kv(1, "33")));
    assertEquals(kv(1, "33"), recordView.getAndReplace(tx, kv(1, "44")));
    assertTrue(recordView.replace(tx, kv(1, "55")));
    assertEquals(kv(1, "55"), recordView.getAndDelete(tx, key));
    assertFalse(recordView.delete(tx, key));
    recordView.upsertAll(tx, List.of(kv(1, "6"), kv(2, "7")));
    assertEquals(2, recordView.getAll(tx, List.of(key, key(2), key(3))).size());
    tx.rollback();
    assertEquals(kv(1, "1"), recordView.get(null, key));
}
Also used : Transaction(org.apache.ignite.tx.Transaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 3 with Transaction

use of org.apache.ignite.tx.Transaction in project ignite-3 by apache.

the class ItThinClientTransactionsTest method testKvViewOperations.

@Test
void testKvViewOperations() {
    KeyValueView<Integer, String> kvView = kvView();
    int key = 1;
    kvView.put(null, key, "1");
    Transaction tx = client().transactions().begin();
    kvView.put(tx, key, "22");
    assertTrue(kvView.contains(tx, key));
    assertFalse(kvView.remove(tx, key, "1"));
    assertFalse(kvView.putIfAbsent(tx, key, "111"));
    assertEquals("22", kvView.get(tx, key));
    assertEquals("22", kvView.getAndPut(tx, key, "33"));
    assertEquals("33", kvView.getAndReplace(tx, key, "44"));
    assertTrue(kvView.replace(tx, key, "55"));
    assertEquals("55", kvView.getAndRemove(tx, key));
    assertFalse(kvView.contains(tx, key));
    assertFalse(kvView.remove(tx, key));
    kvView.putAll(tx, Map.of(key, "6", 2, "7"));
    assertEquals(2, kvView.getAll(tx, List.of(key, 2, 3)).size());
    tx.rollback();
    assertEquals("1", kvView.get(null, key));
}
Also used : Transaction(org.apache.ignite.tx.Transaction) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 4 with Transaction

use of org.apache.ignite.tx.Transaction in project ignite-3 by apache.

the class TxAbstractTest method testConcurrent.

@Test
public void testConcurrent() throws TransactionException {
    Transaction tx = igniteTransactions.begin();
    Transaction tx2 = igniteTransactions.begin();
    Tuple key = makeKey(1);
    Tuple val = makeValue(1, 100.);
    accounts.recordView().upsert(null, val);
    var table = accounts.recordView();
    var table2 = accounts.recordView();
    assertEquals(100., table.get(tx, key).doubleValue("balance"));
    assertEquals(100., table2.get(tx2, key).doubleValue("balance"));
    tx.commit();
    tx2.commit();
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Transaction(org.apache.ignite.tx.Transaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 5 with Transaction

use of org.apache.ignite.tx.Transaction in project ignite-3 by apache.

the class TxAbstractTest method testGetAllAbort.

/**
 * Tests if a transaction is rolled back if one of the batch keys can't be locked.
 */
@Test
public void testGetAllAbort() throws TransactionException {
    List<Tuple> keys = List.of(makeKey(1), makeKey(2));
    accounts.recordView().upsertAll(null, List.of(makeValue(1, 100.), makeValue(2, 200.)));
    Transaction tx = igniteTransactions.begin();
    RecordView<Tuple> txAcc = accounts.recordView();
    txAcc.upsert(tx, makeValue(1, 300.));
    validateBalance(txAcc.getAll(tx, keys), 300., 200.);
    tx.rollback();
    validateBalance(accounts.recordView().getAll(null, keys), 100., 200.);
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Transaction(org.apache.ignite.tx.Transaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Aggregations

Transaction (org.apache.ignite.tx.Transaction)18 Test (org.junit.jupiter.api.Test)16 Tuple (org.apache.ignite.table.Tuple)6 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)5 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 TransactionException (org.apache.ignite.tx.TransactionException)4 IgniteClientException (org.apache.ignite.client.IgniteClientException)2 IgniteException (org.apache.ignite.lang.IgniteException)2 IgniteClient (org.apache.ignite.client.IgniteClient)1