Skip to content

Commit ed78f23

Browse files
committed
Removing finalize fixes broken tests, but no idea why it is called when
there still live references.
1 parent 8e99b2f commit ed78f23

File tree

1 file changed

+67
-75
lines changed

1 file changed

+67
-75
lines changed

src/main/java/org/mybatis/caches/redis/RedisCache.java

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@
3030
*/
3131
public final class RedisCache implements Cache {
3232

33-
private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
34-
35-
private String id;
36-
37-
private static JedisPool pool;
38-
39-
public RedisCache(final String id) {
40-
if (id == null) {
41-
throw new IllegalArgumentException("Cache instances require an ID");
42-
}
43-
this.id = id;
44-
ConfigWithHost configWithHost = RedisConfigurationBuilder.getInstance().parseConfiguration();
45-
pool = new JedisPool(configWithHost, configWithHost.getHost());
46-
}
47-
48-
private Object execute(RedisCallback callback) {
49-
Jedis jedis = pool.getResource();
50-
try {
51-
return callback.doWithRedis(jedis);
52-
} finally {
33+
private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
34+
35+
private String id;
36+
37+
private static JedisPool pool;
38+
39+
public RedisCache(final String id) {
40+
if (id == null) {
41+
throw new IllegalArgumentException("Cache instances require an ID");
42+
}
43+
this.id = id;
44+
ConfigWithHost configWithHost = RedisConfigurationBuilder.getInstance().parseConfiguration();
45+
pool = new JedisPool(configWithHost, configWithHost.getHost());
46+
}
47+
48+
private Object execute(RedisCallback callback) {
49+
Jedis jedis = pool.getResource();
50+
try {
51+
return callback.doWithRedis(jedis);
52+
} finally {
5353
jedis.close();
54-
}
54+
}
5555
}
5656

57-
public String getId() {
58-
return this.id;
59-
}
57+
public String getId() {
58+
return this.id;
59+
}
6060

61-
public int getSize() {
61+
public int getSize() {
6262
return (Integer) execute(new RedisCallback() {
6363
public Object doWithRedis(Jedis jedis) {
6464
Map<byte[], byte[]> result = jedis.hgetAll(id.toString().getBytes());
@@ -67,56 +67,48 @@ public Object doWithRedis(Jedis jedis) {
6767
});
6868
}
6969

70-
public void putObject(final Object key, final Object value) {
71-
execute(new RedisCallback() {
72-
public Object doWithRedis(Jedis jedis) {
73-
jedis.hset(id.toString().getBytes(), key.toString().getBytes(), SerializeUtil.serialize(value));
74-
return null;
75-
}
76-
});
77-
}
78-
79-
public Object getObject(final Object key) {
80-
return execute(new RedisCallback() {
81-
public Object doWithRedis(Jedis jedis) {
82-
return SerializeUtil.unserialize(jedis.hget(id.toString().getBytes(), key.toString().getBytes()));
83-
}
84-
});
85-
}
86-
87-
public Object removeObject(final Object key) {
88-
return execute(new RedisCallback() {
89-
public Object doWithRedis(Jedis jedis) {
90-
return jedis.hdel(id.toString(), key.toString());
91-
}
92-
});
93-
}
94-
95-
public void clear() {
96-
execute(new RedisCallback() {
97-
public Object doWithRedis(Jedis jedis) {
98-
jedis.del(id.toString());
99-
return null;
100-
}
101-
});
102-
103-
}
104-
105-
public ReadWriteLock getReadWriteLock() {
106-
return readWriteLock;
107-
}
108-
109-
@Override
110-
public String toString() {
111-
return "Redis {"
112-
+ id
113-
+ "}";
114-
}
70+
public void putObject(final Object key, final Object value) {
71+
execute(new RedisCallback() {
72+
public Object doWithRedis(Jedis jedis) {
73+
jedis.hset(id.toString().getBytes(), key.toString().getBytes(), SerializeUtil.serialize(value));
74+
return null;
75+
}
76+
});
77+
}
78+
79+
public Object getObject(final Object key) {
80+
return execute(new RedisCallback() {
81+
public Object doWithRedis(Jedis jedis) {
82+
return SerializeUtil.unserialize(jedis.hget(id.toString().getBytes(), key.toString().getBytes()));
83+
}
84+
});
85+
}
86+
87+
public Object removeObject(final Object key) {
88+
return execute(new RedisCallback() {
89+
public Object doWithRedis(Jedis jedis) {
90+
return jedis.hdel(id.toString(), key.toString());
91+
}
92+
});
93+
}
94+
95+
public void clear() {
96+
execute(new RedisCallback() {
97+
public Object doWithRedis(Jedis jedis) {
98+
jedis.del(id.toString());
99+
return null;
100+
}
101+
});
102+
103+
}
104+
105+
public ReadWriteLock getReadWriteLock() {
106+
return readWriteLock;
107+
}
115108

116-
@Override
117-
protected void finalize() throws Throwable {
118-
pool.destroy();
119-
super.finalize();
120-
}
109+
@Override
110+
public String toString() {
111+
return "Redis {" + id + "}";
112+
}
121113

122114
}

0 commit comments

Comments
 (0)