Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make jedis pool return jedis pooled connection object. #345

Open
wants to merge 2 commits into from

3 participants

@mpillar

And some small build changes.

mpillar added some commits
@mpillar mpillar Add maven assembly plugin to pom.xml. 5362e26
@mpillar mpillar Make jedis pool return jedis pooled connection object.
This allows us to call returnResource() on the connection object as
opposed to having to pass the pool around for the return. JDBC style,
just a little more explicit.
94e8ee4
@fbettag

This is very nice and also fixes an issue where returnResource() is an ambiguous reference.

[error]  ambiguous reference to overloaded definition,
[error] both method returnResource in class JedisPool of type (x$1: redis.clients.jedis.BinaryJedis)Unit
[error] and  method returnResource in class Pool of type (x$1: redis.clients.jedis.Jedis)Unit
[error] match argument types (redis.clients.jedis.Jedis)
[error]     pool.returnResource(jedis: Jedis)
@deserat

Having the issue mentioned by @fbettag it's related to scala. anybody know a work around?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 14, 2012
  1. @mpillar
Commits on Sep 29, 2012
  1. @mpillar

    Make jedis pool return jedis pooled connection object.

    mpillar authored
    This allows us to call returnResource() on the connection object as
    opposed to having to pass the pool around for the return. JDBC style,
    just a little more explicit.
This page is out of date. Refresh to see the latest.
View
13 pom.xml
@@ -10,7 +10,7 @@
<packaging>jar</packaging>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
- <version>2.2.0-SNAPSHOT</version>
+ <version>2.2.1</version>
<name>Jedis</name>
<description>Jedis is a blazingly small and sane Redis java client.</description>
<url>http://code.google.com/p/jedis/</url>
@@ -66,7 +66,16 @@
</dependencies>
<build>
- <plugins>
+ <plugins>
+ <plugin>
+ <!-- (mvn compile) mvn assembly:single -->
+ <artifactId>maven-assembly-plugin</artifactId>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
View
138 src/main/java/redis/clients/jedis/JedisPool.java
@@ -8,81 +8,105 @@
import redis.clients.util.Pool;
-public class JedisPool extends Pool<Jedis> {
-
- public JedisPool(final Config poolConfig, final String host) {
- this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
+public class JedisPool extends Pool<JedisPooledConnection>
+{
+ public JedisPool(final Config poolConfig, final String host)
+ {
+ this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
+ Protocol.DEFAULT_DATABASE);
}
- public JedisPool(String host, int port) {
+ public JedisPool(String host, int port)
+ {
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}
- public JedisPool(final String host) {
- URI uri = URI.create(host);
- if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
- String h = uri.getHost();
- int port = uri.getPort();
- String password = uri.getUserInfo().split(":", 2)[1];
- int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
- this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
- Protocol.DEFAULT_TIMEOUT, password, database), new Config());
- } else {
- this.internalPool = new GenericObjectPool(new JedisFactory(host,
- Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
- Protocol.DEFAULT_DATABASE), new Config());
- }
+ public JedisPool(final String host)
+ {
+ URI uri = URI.create(host);
+ if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
+ String h = uri.getHost();
+ int port = uri.getPort();
+ String password = uri.getUserInfo().split(":", 2)[1];
+ int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
+
+ JedisFactory factory =
+ new JedisFactory(this, h, port, Protocol.DEFAULT_TIMEOUT, password, database);
+ this.internalPool = new GenericObjectPool(factory, new Config());
+ } else {
+ JedisFactory factory =
+ new JedisFactory(this, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
+ Protocol.DEFAULT_DATABASE);
+
+ this.internalPool = new GenericObjectPool(factory, new Config());
+ }
}
- public JedisPool(final URI uri) {
- String h = uri.getHost();
- int port = uri.getPort();
- String password = uri.getUserInfo().split(":", 2)[1];
- int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
- this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
- Protocol.DEFAULT_TIMEOUT, password, database), new Config());
+ public JedisPool(final URI uri)
+ {
+ String h = uri.getHost();
+ int port = uri.getPort();
+ String password = uri.getUserInfo().split(":", 2)[1];
+ int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
+
+ JedisFactory factory =
+ new JedisFactory(this, h, port, Protocol.DEFAULT_TIMEOUT, password, database);
+
+ this.internalPool = new GenericObjectPool(factory, new Config());
}
-
+
public JedisPool(final Config poolConfig, final String host, int port,
- int timeout, final String password) {
+ int timeout, final String password)
+ {
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
}
- public JedisPool(final Config poolConfig, final String host, final int port) {
+ public JedisPool(final Config poolConfig, final String host, final int port)
+ {
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
}
- public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
+ public JedisPool(final Config poolConfig, final String host, final int port,
+ final int timeout)
+ {
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
}
- public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
- final int database) {
- super(poolConfig, new JedisFactory(host, port, timeout, password, database));
+ public JedisPool(final Config poolConfig, final String host, int port, int timeout,
+ final String password, final int database)
+ {
+ JedisFactory factory = new JedisFactory(this, host, port, timeout, password, database);
+ this.internalPool = new GenericObjectPool(factory, poolConfig);
}
-
- public void returnBrokenResource(final BinaryJedis resource) {
- returnBrokenResourceObject(resource);
+ public void returnBrokenResource(final BinaryJedis resource)
+ {
+ returnBrokenResourceObject(resource);
}
-
- public void returnResource(final BinaryJedis resource) {
- returnResourceObject(resource);
+
+ public void returnResource(final BinaryJedis resource)
+ {
+ returnResourceObject(resource);
}
-
+
/**
* PoolableObjectFactory custom impl.
*/
- private static class JedisFactory extends BasePoolableObjectFactory {
+ private static class JedisFactory extends BasePoolableObjectFactory
+ {
+ private JedisPool pool;
private final String host;
private final int port;
private final int timeout;
private final String password;
private final int database;
- public JedisFactory(final String host, final int port,
- final int timeout, final String password, final int database) {
+ public JedisFactory(JedisPool pool, final String host, final int port, final int timeout,
+ final String password, final int database)
+ {
super();
+
+ this.pool = pool;
this.host = host;
this.port = port;
this.timeout = timeout;
@@ -90,21 +114,26 @@ public JedisFactory(final String host, final int port,
this.database = database;
}
- public Object makeObject() throws Exception {
- final Jedis jedis = new Jedis(this.host, this.port, this.timeout);
+ public Object makeObject() throws Exception
+ {
+ assert pool != null;
+ final JedisPooledConnection jpc = new JedisPooledConnection(pool, this.host, this.port,
+ this.timeout);
- jedis.connect();
+ jpc.connect();
if (null != this.password) {
- jedis.auth(this.password);
+ jpc.auth(this.password);
}
- if( database != 0 ) {
- jedis.select(database);
+
+ if (database != 0) {
+ jpc.select(database);
}
-
- return jedis;
+
+ return jpc;
}
- public void destroyObject(final Object obj) throws Exception {
+ public void destroyObject(final Object obj) throws Exception
+ {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
if (jedis.isConnected()) {
@@ -112,7 +141,9 @@ public void destroyObject(final Object obj) throws Exception {
try {
jedis.quit();
} catch (Exception e) {
+ // Nothing to do.
}
+
jedis.disconnect();
} catch (Exception e) {
@@ -121,7 +152,8 @@ public void destroyObject(final Object obj) throws Exception {
}
}
- public boolean validateObject(final Object obj) {
+ public boolean validateObject(final Object obj)
+ {
if (obj instanceof Jedis) {
final Jedis jedis = (Jedis) obj;
try {
View
17 src/main/java/redis/clients/jedis/JedisPooledConnection.java
@@ -0,0 +1,17 @@
+package redis.clients.jedis;
+
+public class JedisPooledConnection extends Jedis
+{
+ private final JedisPool pool;
+
+ public JedisPooledConnection(JedisPool pool, String host, int port, int timeout)
+ {
+ super(host, port, timeout);
+ this.pool = pool;
+ }
+
+ public void returnResource()
+ {
+ pool.returnResource(this);
+ }
+}
Something went wrong with that request. Please try again.