简体   繁体   中英

Closing assigned instance variable

I'm working with java and databases. I have a connections class PgConnection and within this class I have a private connection variable

private Connection Con = null;

Within the constructor the Connection is created

Properties props = new Properties();
    props.load(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("database.properties"));

    String url = props.getProperty("db.url");
    String user = props.getProperty("db.user");
    String passwd = props.getProperty("db.passwd");

    Class.forName("org.postgresql.Driver");
    this.con = DriverManager.getConnection(url, user, passwd);

When looking to use this con variable throughout my code I have been referencing it by assigning it to a local variable within a function Connection conn = this.con;

Is this correct practice? And if so, should I be closing conn before exiting the function. Once I am finished with a particular instance of PgConnection I have a Close() method that closes this.con

Thanks

Of course it is by far much safer and easier to handle if you close the connection within the same method that has opened it, because then your method keeps control of the lifecycle of your database connection. If that's possible, your method should always make use of the try-with-resources statement , to make sure, the connection is closed even in case of exceptions:

public void performDatabaseOperation(...) {
    // initialize properties here, load driver etc. here

    try (Connection con = DriverManager.getConnection(url, user, pwd)) {
        // use connection.
        // it will be closed automatically at the end of this code block
        // even if exceptions occure!
    }
}

Of course, if you want to give more control to the calling code, you can separate connection opening from closing using the approach you mentioned. But bear in mind that this will introduce state and the danger that the calling code does not call close properly/at the correct point of time.

One example when you will probably have to give lifecycle-control to your caller is when your API offers several methods that operate on the database and can be called in any arbitrary order, and all operations should run within the same transaction / using the same connection.

Just think about the advantage of having two separate methods open() and close() in opposite to have one single perform() method in your individual case. If there is none, keep it simple!

Yes you need to close connection every time unless its persistent unit connection. Basically you can write one method to get DB connection for any operation and connection should be closed inside your method where you put DB related stuff.

Refer this link for more info.

If you close your local conn before exiting the method, your original Connection instance con to which your local conn refers will also get closed and you cannot make any further operations using that connection.

The main point to consider is that here conn and con refers to the same Connection instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM