java - Maintaining multiple JDBC connections in Struts2 -


the number of connections db exceeds permissible limit.

this tried far.

when user logs in add 1 connection object session:

connection conn = databaseconnectionmanager.getconnection(); sessionmap.put("connection", conn); 

then, whenever need db connection, fetch session:

map<string, object> sessionmap = (map<string, object>) actioncontext.getcontext().get("session"); connection conn = (connection) sessionmap.get("connection"); 

in getconnection() method print number of times method called. although fetch connection object session why number of connections exceed permissible limit 50 ?

jndi code:

connection conn = null;          try {     context initctx = new initialcontext();     context envctx = (context) initctx.lookup("java:comp/env");     datasource ds = (datasource) envctx.lookup("jdbc/mysqdb");     conn = ds.getconnection();  }   catch (namingexception ex) {     logger.getlogger(databaseconnectionmanager.class.getname()).log(level.severe, null, ex); } catch (sqlexception sqle) {     sqle.printstacktrace(); } system.out.println("connection: "+connection++); return conn; 

i have used jndi. number of connections within permissible limit. i'm not sure if right way, please advise.

one connection each user not solution @ all. mentioned must use connection poll , if want simple use https://commons.apache.org/proper/commons-dbcp/ . mentioned there:

creating new connection each user can time consuming (often requiring multiple seconds of clock time), in order perform database transaction might take milliseconds. opening connection per user can unfeasible in publicly-hosted internet application number of simultaneous users can large. accordingly, developers wish share "pool" of open connections between of application's current users. number of users performing request @ given time small percentage of total number of active users, , during request processing time database connection required. application logs dbms, , handles user account issues internally.

you can find samples @ http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -