为什么Jedis操作后要主动归还?
生活随笔
收集整理的這篇文章主要介紹了
为什么Jedis操作后要主动归还?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
現階段,互聯網越來越熱,高并發的設計越來越高,大多數開發者都開始使用redis作為中間件,用以解決高并發的問題。jedis作為redis的客戶端,使用也是越來越頻繁。很多開發者都注意到了無論是在單機模式還是分布式模式下,jedis獲得鏈接完成操作后,都必須主動歸還(也就是做鏈接的關閉)動作。為什么呢?
查看jedis源碼不難發現其緣由。
public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands,AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands { ........ }Jedis繼承了父類BinaryJedis,那么再來看看BinaryJedis源碼:
public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands,AdvancedBinaryJedisCommands, BinaryScriptingCommands, Closeable {protected Client client = null;protected Transaction transaction = null;protected Pipeline pipeline = null;......}查看BinaryJedis源碼,不難發現,所有與redis操作都與Client類有關。我們就再來看看Client這個類吧。
public class Client extends BinaryClient implements Commands {public Client() {super();}public Client(final String host) {super(host);} ....... }繼續查看BinaryClient類
public class BinaryClient extends Connection {public enum LIST_POSITION {BEFORE, AFTER;public final byte[] raw;private LIST_POSITION() {raw = SafeEncoder.encode(name());}}private boolean isInMulti;private String password;private long db;private boolean isInWatch;public BinaryClient() {super();}public BinaryClient(final String host) {super(host);} .......... }還有父類,繼續查看父類Connection
public class Connection implements Closeable {private static final byte[][] EMPTY_ARGS = new byte[0][];private String host = Protocol.DEFAULT_HOST;private int port = Protocol.DEFAULT_PORT;private Socket socket;private RedisOutputStream outputStream;private RedisInputStream inputStream;private int pipelinedCommands = 0;private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;private int soTimeout = Protocol.DEFAULT_TIMEOUT;private boolean broken = false;private boolean ssl;private SSLSocketFactory sslSocketFactory;private SSLParameters sslParameters;private HostnameVerifier hostnameVerifier;public Connection() {}public Connection(final String host) {this.host = host;}public Connection(final String host, final int port) {this.host = host;this.port = port;}public Connection(final String host, final int port, final boolean ssl) {this.host = host;this.port = port;this.ssl = ssl;}......... }吼吼,看到這里就不難發現,jedis作為redis的客戶端鏈接服務器是通過socket來完成的。
而Redis采用的是基于內存的采用的是單進程單線程模型的KV數據庫。所以作為多線程的客戶端,jedis使用后要主動做歸還動作。否則。。。你明白的!占用socket資源咯
轉載于:https://my.oschina.net/OHC1U9jZt/blog/1537428
總結
以上是生活随笔為你收集整理的为什么Jedis操作后要主动归还?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中JRE、JDK和JVM的区别
- 下一篇: 内存分配与数据格式化(malloc与ne