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

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

Introduction

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

Prototype

Object getNativeConnection();

Source Link

Document

Returns the native connection (the underlying library/driver object).

Usage

From source file:org.springframework.data.redis.connection.jredis.JRedisConnectionIntegrationTests.java

@Test
public void testConnectionNotReturnedOnException() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);/*from w  w  w .  j a v a  2s.co m*/
    config.setMaxWaitMillis(1);
    JredisConnectionFactory factory2 = new JredisConnectionFactory(
            new JredisPool(SettingsUtils.getHost(), SettingsUtils.getPort(), config));
    RedisConnection conn2 = factory2.getConnection();
    ((JRedis) conn2.getNativeConnection()).quit();
    try {
        conn2.ping();
        fail("Expected RedisConnectionFailureException trying to use a closed connection");
    } catch (RedisConnectionFailureException e) {
    }
    conn2.close();
    // Verify we get a new connection from the pool and not the broken one
    RedisConnection conn3 = factory2.getConnection();
    conn3.ping();
}

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

@Test
public void testValidateNoError() {
    factory.setValidateConnection(true);
    RedisConnection conn2 = factory.getConnection();
    assertSame(connection.getNativeConnection(), conn2.getNativeConnection());
}

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

@SuppressWarnings("unchecked")
@Test//from   w  w w  . j a  v  a 2  s  .  c o m
public void testDisableSharedConnection() throws Exception {
    factory.setShareNativeConnection(false);
    RedisConnection conn2 = factory.getConnection();
    assertNotSame(connection.getNativeConnection(), conn2.getNativeConnection());
    // Give some time for native connection to asynchronously initialize, else close doesn't work
    Thread.sleep(100);
    conn2.close();
    assertTrue(conn2.isClosed());
    // Give some time for native connection to asynchronously close
    Thread.sleep(100);
    try {
        ((RedisAsyncCommands<byte[], byte[]>) conn2.getNativeConnection()).ping();
        fail("The native connection should be closed");
    } catch (RedisException e) {
        // expected
    }
}

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

@SuppressWarnings("unchecked")
@Test// w  ww. ja v a  2  s.  c o  m
public void testInitConnection() {
    RedisAsyncCommands<byte[], byte[]> nativeConn = (RedisAsyncCommands<byte[], byte[]>) connection
            .getNativeConnection();
    factory.initConnection();
    RedisConnection newConnection = factory.getConnection();
    assertNotSame(nativeConn, newConnection.getNativeConnection());
    newConnection.close();
}

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

@SuppressWarnings("unchecked")
@Test/*  w  ww  .  jav a  2 s  .  c  o m*/
public void testResetAndInitConnection() {
    RedisAsyncCommands<byte[], byte[]> nativeConn = (RedisAsyncCommands<byte[], byte[]>) connection
            .getNativeConnection();
    factory.resetConnection();
    factory.initConnection();
    RedisConnection newConnection = factory.getConnection();
    assertNotSame(nativeConn, newConnection.getNativeConnection());
    newConnection.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);/*from www .j  a va 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();
}