UnderTaker UnderTaker - 2 months ago 8
Java Question

Why is the sql connection getting created in new thread?

I have a couple of questions on the class below.

public class Test {
public void run() throws SQLException {
Connection conn = getConnection();
DSLContext create = DSL.using(conn, SQLDialect.MYSQL);

// query books for author named 'selena'
Result<Record2<Long, String>> result = create
.select(BOOK.ID, BOOK.TITLE).from(BOOK).join(BOOK_AUTHOR_REL)
.on(BOOK_AUTHOR_REL.BOOKID.equal(BOOK.ID)).join(AUTHOR)
.on(BOOK_AUTHOR_REL.AUTHORID.equal(AUTHOR.ID))
.where(AUTHOR.NAME.equal("selena"))
.orderBy(BOOK.TITLE.asc(), BOOK.ID.asc()).fetch();
result.forEach((r) -> {
System.out.println(String.format("%s (id: %s)",
r.getValue(BOOK.TITLE), r.getValue(BOOK.ID)));
});
conn.close();
System.exit(0);
}

public static void main(final String[] args) throws SQLException {
new Test().run();
}

private Connection getConnection() {
try {
Class.forName(System.getProperty("jdbc.driver")).newInstance();
return DriverManager.getConnection(System.getProperty("jdbc.url"),
System.getProperty("jdbc.user"),
System.getProperty("jdbc.password"));
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return null;
}
}



  1. Why is the new connection getting created in a new thread? Why do we call it
    void run
    not in any other method which belongs to instance of class?

  2. Why is the String argument
    final
    here?

    public static void main(final String[] args) throws SQLException {
    new Test().run();
    }

  3. After closing the connection, why are we calling
    System.exit()
    ?

    conn.close();
    System.exit(0);


Answer

It's not created in a new thread. In order to call this in a new thread, then Test would have to implement Runnable (or extend Thread). Assuming Test implemented Runnable there would be a line like new Thread(new Test()).start();.

There's a convention that an argument to a method shouldn't be modified. Using final prevents that, but it is unnecessary here, since the JVM won't do anything with args after calling main. Also note making the array final doesn't make its contents unmodifiable.

Calling System.exit is unnecessary here. The code is done executing anyway, the JVM will terminate once there's no more code to run. System.exit abruptly kills the JVM.

This is not great code. Having the getConnection catch exceptions and return null is bad style (because the rest of the program doesn't know the connection is null and will try to access it, causing a NullPointerException), it could be changed to:

private Connection getConnection() throws SQLException {
    return DriverManager.getConnection(System.getProperty("jdbc.url"),
            System.getProperty("jdbc.user"),
            System.getProperty("jdbc.password"));
}

Class.forName is unnecessary with a type 4 jdbc driver, and calling newInstance doesn't do anything useful.

Also the connection doesn't get closed if an exception is thrown, the connection on the server side will be left to time-out.

This is plumbing code done to demonstrate the DSL class. I wouldn't worry too much about it.

Comments