Search in sources :

Example 36 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method testResetStateWhenInWatch.

@Test
public void testResetStateWhenInWatch() {
    jedis.watch("mykey", "somekey");
    // state reset : unwatch
    jedis.resetState();
    Transaction t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set("mykey", "bar");
    nj.disconnect();
    t.set("mykey", "foo");
    List<Object> resp = t.exec();
    assertNotNull(resp);
    assertEquals(1, resp.size());
    assertEquals("foo", jedis.get("mykey"));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 37 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class TransactionCommandsTest method transactionResponseWithError.

@Test
public void transactionResponseWithError() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    Response<Set<String>> error = t.smembers("foo");
    Response<String> r = t.get("foo");
    List<Object> l = t.exec();
    assertEquals(JedisDataException.class, l.get(1).getClass());
    try {
        error.get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals(r.get(), "bar");
}
Also used : Set(java.util.Set) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Example 38 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class JedisSentinelPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    Jedis jedis2 = null;
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
        jedis.close();
        jedis2 = pool.getResource();
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } catch (JedisConnectionException e) {
        if (jedis2 != null) {
            jedis2 = null;
        }
    } finally {
        jedis2.close();
        pool.destroy();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 39 with Transaction

use of redis.clients.jedis.Transaction in project jedis by xetorthio.

the class JedisPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } finally {
        jedis2.close();
    }
    pool.destroy();
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Example 40 with Transaction

use of redis.clients.jedis.Transaction in project opennms by OpenNMS.

the class RedisResourceMetadataCache method merge.

@Override
public void merge(Context context, Resource resource, ResourceMetadata metadata) {
    final Optional<ResourceMetadata> o = get(context, resource);
    try (Jedis jedis = m_pool.getResource()) {
        if (!o.isPresent()) {
            final ResourceMetadata newMetadata = new ResourceMetadata();
            newMetadata.merge(metadata);
            final Transaction t = jedis.multi();
            final byte[] key = key(METADATA_PREFIX, context.getId(), resource.getId());
            t.set(key, conf.asByteArray(newMetadata));
            // Index the key, element by element, in order to support calls to getResourceIdsWithPrefix()
            final List<String> elements = Lists.newArrayList(SEARCH_PREFIX, context.getId());
            for (String el : m_resourceIdSplitter.splitIdIntoElements(resource.getId())) {
                elements.add(el);
                t.lpush(m_resourceIdSplitter.joinElementsToId(elements).getBytes(), key);
            }
            // Update the keys in transaction
            t.exec();
        } else if (o.get().merge(metadata)) {
            // Update the value stored in the cache if it was changed as a result of the merge
            jedis.set(key(METADATA_PREFIX, context.getId(), resource.getId()), conf.asByteArray(o.get()));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata)

Aggregations

Transaction (redis.clients.jedis.Transaction)42 Test (org.junit.Test)32 Jedis (redis.clients.jedis.Jedis)19 Set (java.util.Set)7 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)6 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Response (redis.clients.jedis.Response)3 ArrayList (java.util.ArrayList)2 JedisPool (redis.clients.jedis.JedisPool)2 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 TicketModel (com.gitblit.models.TicketModel)1 Change (com.gitblit.models.TicketModel.Change)1 ResourceMetadata (org.opennms.newts.cassandra.search.ResourceMetadata)1