java - Why is the sql connection getting created in new thread? -



java - Why is the sql connection getting created in new thread? -

i have couple of question on class below.

public class test { public void run() throws sqlexception { connection conn = getconnection(); dslcontext create = dsl.using(conn, sqldialect.mysql); // query books 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() { seek { class.forname(system.getproperty("jdbc.driver")).newinstance(); homecoming drivermanager.getconnection(system.getproperty("jdbc.url"), system.getproperty("jdbc.user"), system.getproperty("jdbc.password")); } grab (instantiationexception | illegalaccessexception | classnotfoundexception | sqlexception e) { e.printstacktrace(); } homecoming null; } } why new connection getting created in new thread? why phone call void run not in other method belongs instance of class?

why string argument final here?

public static void main(final string[] args) throws sqlexception { new test().run(); }

after closing connection, why calling system.exit()?

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

it's not created in new thread. in order phone call in new thread, test have implement runnable (or extend thread). assuming test implemented runnable there line new thread(new test()).start();.

there's convention argument method shouldn't modified. using final prevents that, unnecessary here, since jvm won't args after calling main. note making array final doesn't create contents unmodifiable.

calling system.exit unnecessary here. code done executing anyway, jvm terminate 1 time there's no more code run. system.exit abruptly kills jvm.

this not great code. having getconnection grab exceptions , homecoming null bad style (because rest of programme doesn't know connection null , seek access it, causing nullpointerexception), changed to:

private connection getconnection() throws sqlexception { homecoming drivermanager.getconnection(system.getproperty("jdbc.url"), system.getproperty("jdbc.user"), system.getproperty("jdbc.password")); }

class.forname unnecessary type 4 jdbc driver, , calling newinstance doesn't useful.

also connection doesn't closed if exception thrown, connection on server side left time-out.

this plumbing code done demonstrate dsl class. wouldn't worry much it.

java multithreading

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -