Question Best Practices Question.. SSH Tunnel

big.nerd

New member
Joined
Feb 20, 2014
Messages
3
Programming Experience
10+
Hello All,

I will have to admit I am new (not only here, but to VB.NET) and I am working on a small purpose built app.

Essentially - it's a front-end to allow one of my customers to make certain changes to our MySQL Database, everything is working as expected, however I have now come to require an SSH Tunnel.

I've already used SSH.NET and was able to get the tunnel created, connect to mysql, execute query, etc..

I am looking for what the best practices is for keeping the tunnel open during the duration of the application.

For instance - right now I have assigned the code under a button - it creates the tunnel, connects, etc.

Rather than modifying the numerous points in the code where specific queries are performed to check if the tunnel is required, create, then continue with what is already done and working, I wanted to have the tunnel connected while the application is open.

My first instinct is to create a Background Worker, with the BackgroundWorker object sitting at class level (as per recommendation from Microsoft), as well as the client and portforward object for the SSH tunnel which should allow me to access it throughout the class, including checking if it's active, etc.

I guess .. so I stop rambling, what would be the best practice for executing this type of action? Am I on the right track with creating a background worker so it will continue to "wait" while other actions are performed, etc?

Thanks in advance for any advice anyone can give .. code of course is always appreciated but I would like to learn the best possible way, so looking for advice.
 
Best practice would be to keep the connection opened only when you actually need it... HOWEVER if you need it often, it might be best to keep it opened the whole time to keep performance adequate... If it's anything slower than a couple queries per minute I would open the connection, run the query(ies), then close it.
 
JohnH / Herman,

Thank you for your replies!

Wonderful - okay there isn't that much, I was just trying to think of the best way to go.

I suppose then it would be better if I did the following:

* Create reusable code to actually execute the query (this is not the case right now), i.e. create a class that will execute the query and respond accordingly, such as returning a Boolean for success or not (there is no select here).

* Within that reusable code, have the switch which looks for the condition requiring the SSH Tunnel, then activating it, etc..

Anything else?

I am actually trying to force myself to start using proper techniques, so anything else anyone can think of to push me in the right direction, I'd appreciate it.

Michael (big.nerd)
 
Last edited:
Back
Top