java - When to close SQL Connection with DAO pattern? -
i building rest
service contains code looks this:
@put @produces(mediatype.application_xml) public response updateorder( @headerparam("user") string user ){ coffeeorderdao coffeedao = new coffeeorderdaoimpl(); .... coffeedao.method1(); .... coffeedao.method2(); ....
coffeeorderdaoimpl
public class coffeeorderdaoimpl implements coffeeorderdao{ context ctx; datasource ds; connection conn; public coffeeorderdaoimpl(){ seek { ctx = new initialcontext(); ds = (datasource)ctx.lookup("java:comp/env/jdbc/coffeedb"); conn = ds.getconnection(); } catch(sqlexception e){ system.out.println("exception:" + e); } grab (namingexception ne) { } } public string method1() { } public string method2() { }
my question should closing connection in both method1()
, method2()
, create new coffeeorderdao
object before calling these 2 methods?
or
use same coffeeorderdao
object both methods , close connection in method2()
the sec alternative seems odd in becomes more complicated know when connections closed.
so proper create multiple coffeeorderdao
objects or missing something?
thank help!
if anything, should @ to the lowest degree using abstraction on jdbc. shouldn't handling intricacies of connections on own.
check out this reply suggestion of such tools. tools heavy lifting you.
besides making code lot less error prone, using 1 of these tools create code lot more readable since contain lot less boilerplate code
java mysql jdbc
No comments:
Post a Comment