Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Jsch into connection pool in details

I put Jsch into commons-pool (with spring pool support) with initial success

http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/htmlsingle/#aop-ts-pool

However:

  1. Should we pool the channels within the Session instead of pooling the sessions? Each Jsch session creates one thread. Pooling Jsch sessions will create x threads. Pooling channels, there will really be only one Jsch thread.

  2. (commons-pool) what happens if the Jsch session went stale? How to regenerate the session in the context of the commons-pool or using spring pool support? How to detect whether it goes stale?

Thanks

like image 783
simonso Avatar asked Oct 31 '13 16:10

simonso


People also ask

What do you mean by connection pooling discuss in detail?

Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.

Do we need to close connection in connection pool?

Yes, certainly you need to close the pooled connection as well. It's actually a wrapper around the actual connection. It wil under the covers release the actual connection back to the pool.

What is the use of connection pooling?

Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore.


1 Answers

Figured out my own question. I will share my project in the next day or two.

  1. Pooling channels are much more effective. There is really no need to create multiple sessions (if the session connects to the same sftp endpoint).

  2. I implemented a JSch connection pool (pooling channels) with spring pool and commons-pool. I will post to the github in the next day or two. The most important question is, what if the connection went stale.

I found out that based on my implementation of 1 Session - multiple channels, and if the connection went stale, the pooled objects (in this case, the channel) will be stale. The pooled object should be invalidated and deleted from the pool. When the connection comes back up, and when new application thread "borrows" from the pool, new pool objects will be created.

To validate my observation, my not-so-automated test:

a) Create a set (say 10) of app threads checking out channel resource from the pool. b) Have the thread to sleep 20 seconds c) Create another set of app threads checking out channel resources from the pool.

At a), set breakpoint when i==7, break the connection by "iptable drop (linux) or pfctl -e; pfctl -f /etc/pf.conf (mac, google how to do!)". This first set of app threads will get exception because the channel is broken.

At b), restart the connection

At c), the 2nd set of app threads will be successfully completing the operation because the broken connection has been restored.

like image 64
simonso Avatar answered Oct 06 '22 11:10

simonso