Comments (11)
I don't how we can make this possible. If we hide connection pooling within Jedis class, we will have to borrow a connection on every executed command and maybe even deal with synchronization issues. It will make things even harder.
Now we moved to the Apache Pool, take a look into the docs since it has bunch of features that might help you to deal with keeping track of connection usage.
http://commons.apache.org/pool/apidocs/org/apache/commons/pool/impl/GenericObjectPool.html
from jedis.
Or you could use an approach like this:
https://github.com/xetorthio/jedis/issues#issue/36
from jedis.
Or use simple template/callback "pattern" just like jdbctemplate or hibernatetemplate.
Something like:
public class JedisTemplate implements InitializingBean {
private JedisPool jedisPool = null;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(jedisPool, "Property jedisPool is required");
}
public <T> T doInJedis(JedisCallback<T> jedisCallback) {
boolean broken = false;
Jedis j = null;
try {
j = jedisPool.getResource();
onDoInJedis(j, jedisCallback);
return jedisCallback.run(j);
} catch (Exception e) {
broken = true;
throw new RuntimeException(e.getMessage(), e);
} finally {
if (j != null) {
if (broken) {
jedisPool.returnBrokenResource(j);
} else {
jedisPool.returnResource(j);
}
}
}
}
/**
* Extension point.
*
* @param j
* @param jedisCallback
*/
protected void onDoInJedis(Jedis j, JedisCallback<?> jedisCallback) {
// do nothing
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
And here is simple callback:
public interface JedisCallback<T> {
T run(Jedis j);
}
There is spring-data project which has redis support. I was going to use it and have played for a while but had to fall back to above solution as it is much simpler and spring-data is not mature at the moment.
from jedis.
Maybe it make sense to put this in the Wiki or even in a Gist. If lots of people use we could add this to a contrib project, I don't know if I would add this to the core.
from jedis.
Yeah, this is not for core. Just wrapper - and spring data sounds like ultimate solution.
Maybe I will extract my wrapper classes and create spring-jedis or jedis-spring project someday :)
from jedis.
I would love to see that. I'm using Spring and Jedis too.
Any clever way of configuring the apache pool from Spring xml?
from jedis.
Oh, I see I need to drop it somewhere:
public class JedisPoolFactoryBean implements FactoryBean<JedisPool>, DisposableBean {
private String host;
private int port = Protocol.DEFAULT_PORT;
private JedisPool pool;
@Override
public void destroy() throws Exception {
pool.destroy();
}
@Override
public JedisPool getObject() throws Exception {
pool = new JedisPool(host, port);
pool.init();
return pool;
}
@Override
public Class<?> getObjectType() {
return JedisPool.class;
}
@Override
public boolean isSingleton() {
return true;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
}
and here is xml:
<bean id="jedisPool" class="com.sentaca.support.redis.JedisPoolFactoryBean">
<property name="host" value="localhost"/>
</bean>
and that's almost 90% in terms of LOC of whole "project".
from jedis.
Well, In my case I'm using a ShardedJedisPool, this is with the apache pool, not the old version. I need to set up things like testWhileIdle or maxActive and it looks like I need to use an instance of org.apache.commons.pool.impl.GenericObjectPool.Config for that.
This class doesn't have setters or getters so I had to extend it to provide setters so I could configure an instance and pass as a constructor-arg to my redis.clients.jedis.ShardedJedisPool. I was wondering if there was another way of doing it without extending the Config class.
from jedis.
I would just pull code, fix it and kindly ask mr xetorthio to merge :)
from jedis.
Even better it would be to simply depend only on the ObjectPool interface since one might want to use her own implementation or use the stack-based implementation.
As for Spring configurations, take a look at the Jedis config classes in Spring Data (along with the RedisTemplate & co).
from jedis.
I am closing this issue since it seems we are talking about addind getters and setters to Config object instead of adding a Proxy class.
And I'm still waiting for the pull request! :)
from jedis.
Related Issues (20)
- Is renewSlotCache necessary when received MOVED HOT 3
- Using Chinese query plus summarize syntax, the characters are garbled:�
- client side cache is not invalidated upon key deletion from the same client HOT 1
- unable to connect to Google Memorystore HOT 4
- Could not get a resource from the pool HOT 7
- Jedis4 cannot close SentinelListener connection completely in a JedisSentineled mode when executing method close ,caused connection leak
- redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
- redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool at redis.clients.jedis.util.Pool.getResource(Pool.java:42) ~[jedis-5.1.2.jar:?] at redis.clients.jedis.JedisPool.getResource(JedisPool.java:378) ~[jedis-5.1.2.jar:?] HOT 2
- JedisPooled ## Could not get a resource from the pool HOT 5
- redis.clients.jedis.exceptions.JedisConnectionException: Unexpected end of stream. HOT 4
- Resource leak: JedisPooled does not return connection to the pool on socket exception HOT 1
- Jedis connect failed when using dns resolver in jedis 5.1.2 HOT 5
- Pipeline is invalid HOT 2
- Performance issue version 5.1.3
- Empty lua table returns EmptyMap since 5.0.1
- redis.clients.jedis.exceptions.JedisConnectionException: Failed to connect to any host resolved for DNS name. HOT 1
- Failed to connect to any host resolved for DNS name. HOT 1
- Java 21 and Virtual Threads: Timeout waiting for idle object HOT 2
- Refresh connections when getting JedisMovedDataException during using pipeline HOT 1
- Does jedis support valkey connections ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jedis.