-2

A little bit confused - lets say we create InitialContext and DataSource like this:

InitialContext ctx = new InitialContext();
DataSource ds = ((DataSource) ctx.lookup("jdbc/hsqldb"));

Now when some database operations are made, I suggest ctx should be closed:

 if (ctx != null) {
     ctx.close();
    }

But here InitialContext is simply initialised to null:

import java.sql.*;
import javax.sql.*;
import javax.ejb.*;
import javax.naming.*;

public class DistributedTransactionBean implements SessionBean {

    // ...

    public void ejbCreate() throws CreateException {

        ctx = new InitialContext();
        ds = (DataSource)ctx.lookup("jdbc/distCoffeesDB");
    }

    public void updateTotal(int incr, String cofName, String username,
                            String password)
        throws SQLException {

        Connection con;
        PreparedStatement pstmt;

        try {
            con = ds.getConnection(username, password);
            pstmt = con.prepareStatement("UPDATE COFFEES " +
                        "SET TOTAL = TOTAL + ? " +
                        "WHERE COF_NAME = ?");
            pstmt.setInt(1, incr);
            pstmt.setString(2, cofName);
            pstmt.executeUpdate();
            stmt.close();
        } finally {
            if (con != null) con.close();
        }
    }

    private DataSource ds = null;
    private Context ctx = null;
}

So what is happening when ctx is initiliased to null without closing it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • A question asking for "which way is better" is usually predestined to being closed as primarily opinion-based. This one - though - has a proper answer: _Always close resources, when you are ready with them._ Closing the resource and nulling the variable are two different non-comparable things, by the way. – Seelenvirtuose Aug 03 '17 at 09:41
  • Ok, question changed. – Ernestas Gruodis Aug 03 '17 at 09:43

1 Answers1

1

When do you think "ctx is set to null" ?

From the code you provided it looks that ctx is not "set to null", it is initialised to null.

Then (after initialisation) during creation of the bean, ejbCreate() is called and reference to InitialContext is assigned:

ctx = new InitialContext();

krisp
  • 131
  • 5
  • So the Sun/Oracle page is telling to initialise it to null, instead of closing it. Still, can not figure the difference.. – Ernestas Gruodis Aug 03 '17 at 10:04
  • Please familiarize yourself with how initialization works. For example: [java class member initialisation](https://stackoverflow.com/questions/3735713/java-class-member-initialisation) – krisp Aug 03 '17 at 10:30
  • So if Sun/Oracle here recommends to initiliase to null closable resource, so it means we can initialize to null instead of closing for example InputStream, OutputStream, database Connection and so on. This is not good I think. My question is still not answered. – Ernestas Gruodis Aug 03 '17 at 10:36
  • https://stackoverflow.com/questions/5120961/context-and-initialcontext-should-i-be-calling-the-close-method-on-these-obj – krisp Aug 03 '17 at 10:50