All things are lawful, but not all things are profitable.
Knute Snortum wrote:Here is a way to have only one DB connection. (Others may improve on it.)
Make your static variable conn private. In connect(), test whether conn is null. It will be the first time. If conn is null, get the connection. Regardless (outside the if statement), return conn.
Now when you want a connection to the DB, call connect(); don't access conn directly. connect() will create the connection the first time and just return that connection subsequent times.