jdbc - Caching InitialContext and DataSource in a Java EE web-app -


in following connector/j reference jdbc/mysql suggests cache instances of initialcontext , datasource. making private static instance solve caching? shouldn't 1 have concerned thread-safety (if @ all)? best 'place' cache web-app (restlet + glassfish/java ee + mysql)??

there genericdao class root of data-access classes, speak. having static instances solve problem? force of methods static don't want. suggestions??

thanks!

public void dosomething() throws exception { /* * create jndi initial context able * lookup datasource ** in production-level code, should cached * instance or static variable, can * quite expensive create jndi context. ** note: code works when using servlets * or ejbs in java ee application server. if * using connection pooling in standalone java code, * have create/configure datasources using whatever * mechanisms particular connection pooling library * provides. */ initialcontext ctx = new initialcontext(); /* * lookup datasource, backed pool * application server provides. datasource instances * candidate caching instance * variable, jndi lookups can expensive well. */ datasource ds = (datasource)ctx.lookup("java:comp/env/jdbc/mysqldb");  /* *remaining code here... */     } 

if you're using jax-rs, can use @context annotation.

e.g.

@context private servletcontext context;  @get @path("whatevers") public list<whatever> getwhatevers() {     datasource datasource = config.getinstance(context).getdatasource();     // ... } 

however, if @resource annotation supported on restlet environment, make use of good.

@resource(mappedname="jdbc/mysqldb") private datasource datasource 

this in turn technically better placed in ejb in turn inject @ejb in webservice.

@stateless public class whateverdao {      @resource(mappedname="jdbc/mysqldb")     private datasource datasource      public list<whatever> list() {         // ...     }  } 

with

@ejb private whateverdao whateverdao;  @get @path("whatevers") public list<whatever> getwhatevers() {     return whateverdao.list(); } 

Comments

Popular posts from this blog

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

apache - Add omitted ? to URLs -

php - How can I stop spam on my custom forum/blog? -