Example usage for org.springframework.data.redis.connection RedisConnection close

List of usage examples for org.springframework.data.redis.connection RedisConnection close

Introduction

In this page you can find the example usage for org.springframework.data.redis.connection RedisConnection close.

Prototype

void close() throws DataAccessException;

Source Link

Document

Closes (or quits) the connection.

Usage

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test
public void testCreateFactoryWithPool() {
    DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();/*from w  ww  .ja v a2 s.  c o  m*/
    LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
    factory2.setShutdownTimeout(0);
    factory2.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory2);

    RedisConnection conn2 = factory2.getConnection();
    conn2.close();
    factory2.destroy();
    pool.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Ignore("Redis must have requirepass set to run this test")
@Test//from   w  ww  .  j a va  2  s .  c o  m
public void testConnectWithPassword() {
    factory.setPassword("foo");
    factory.afterPropertiesSet();
    RedisConnection conn = factory.getConnection();
    // Test shared and dedicated conns
    conn.ping();
    conn.bLPop(1, "key".getBytes());
    conn.close();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-667
public void factoryCreatesPooledConnections() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

    LettuceClientConfiguration configuration = LettucePoolingClientConfiguration.builder()
            .poolConfig(poolConfig).clientResources(LettuceTestClientResources.getSharedClientResources())
            .shutdownTimeout(Duration.ZERO).build();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(),
            configuration);/*w  w w  . jav  a 2  s.c o  m*/
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    RedisConnection initial = factory.getConnection();
    Object initialNativeConnection = initial.getNativeConnection();

    initial.close();

    RedisConnection subsequent = factory.getConnection();
    Object subsequentNativeConnection = subsequent.getNativeConnection();

    subsequent.close();

    assertThat(initialNativeConnection, is(subsequentNativeConnection));

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-687
public void connectsThroughRedisSocket() {

    assumeTrue(EpollProvider.isAvailable() || KqueueProvider.isAvailable());
    assumeTrue(new File(SettingsUtils.getSocket()).exists());

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.create();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(SettingsUtils.socketConfiguration(),
            configuration);//  ww w .  j  a  va  2  s  .  c  o  m
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();
    assertThat(connection.ping(), is(equalTo("PONG")));

    connection.close();
    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-762, DATAREDIS-869
public void factoryUsesElastiCacheMasterReplicaConnections() {

    assumeThat(String.format("No slaves connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.SLAVE)
            .build();//from   www  .j a  va  2s  . c  om

    RedisStaticMasterReplicaConfiguration elastiCache = new RedisStaticMasterReplicaConfiguration(
            SettingsUtils.getHost()).node(SettingsUtils.getHost(), SettingsUtils.getPort() + 1);

    LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        assertThat(connection.ping(), is(equalTo("PONG")));
        assertThat(connection.info().getProperty("role"), is(equalTo("slave")));
    } finally {
        connection.close();
    }

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-762, DATAREDIS-869
public void factoryUsesElastiCacheMasterWithoutMaster() {

    assumeThat(/* w w w .  j a  v  a 2s  .  co m*/
            String.format("No replicas connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder()
            .readFrom(ReadFrom.MASTER).build();

    RedisStaticMasterReplicaConfiguration elastiCache = new RedisStaticMasterReplicaConfiguration(
            SettingsUtils.getHost(), SettingsUtils.getPort() + 1);

    LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        connection.ping();
        fail("Expected RedisException: Master is currently unknown");
    } catch (RedisSystemException e) {

        assertThat(e.getCause(), is(instanceOf(RedisException.class)));
        assertThat(e.getCause().getMessage(), containsString("Master is currently unknown"));
    } finally {
        connection.close();
    }

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-580, DATAREDIS-869
public void factoryUsesMasterReplicaConnections() {

    assumeThat(// ww  w.j a  va 2 s. co m
            String.format("No replicas connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.SLAVE)
            .build();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(SettingsUtils.standaloneConfiguration(),
            configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        assertThat(connection.ping(), is(equalTo("PONG")));
        assertThat(connection.info().getProperty("role"), is(equalTo("slave")));
    } finally {
        connection.close();
    }

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-576
public void connectionAppliesClientName() {

    LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
            .clientResources(LettuceTestClientResources.getSharedClientResources()).clientName("clientName")
            .build();/*from  w ww .  java  2 s. co  m*/

    LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(),
            configuration);
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    RedisConnection connection = factory.getConnection();

    assertThat(connection.getClientName(), is(equalTo("clientName")));
    connection.close();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-576
public void getClientNameShouldEqualWithFactorySetting() {

    LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration());
    factory.setClientResources(LettuceTestClientResources.getSharedClientResources());
    factory.setClientName("clientName");
    factory.afterPropertiesSet();//from  www. j a  va  2 s.c  o  m

    ConnectionFactoryTracker.add(factory);

    RedisConnection connection = factory.getConnection();
    assertThat(connection.getClientName(), equalTo("clientName"));

    connection.close();
}

From source file:org.springframework.data.redis.core.RedisConnectionUtils.java

/**
 * Closes the given connection, created via the given factory if not managed externally (i.e. not bound to the
 * thread)./* www  .  java  2 s . c  o  m*/
 * 
 * @param conn the Redis connection to close
 * @param factory the Redis factory that the connection was created with
 */
public static void releaseConnection(RedisConnection conn, RedisConnectionFactory factory) {

    if (conn == null) {
        return;
    }

    RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager
            .getResource(factory);

    if (connHolder != null && connHolder.isTransactionSyncronisationActive()) {
        if (log.isDebugEnabled()) {
            log.debug("Redis Connection will be closed when transaction finished.");
        }
        return;
    }

    // release transactional/read-only and non-transactional/non-bound connections.
    // transactional connections for read-only transactions get no synchronizer registered
    if (isConnectionTransactional(conn, factory)
            && TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
        unbindConnection(factory);
    } else if (!isConnectionTransactional(conn, factory)) {
        if (log.isDebugEnabled()) {
            log.debug("Closing Redis Connection");
        }
        conn.close();
    }
}