Thanks! > Date: Wed, 17 Sep 2008 08:01:01 -0400 > From: [EMAIL PROTECTED] > To: users@tomcat.apache.org > Subject: Re: Tomcat 5.5, JNDI Connection Pooling, Active connections keep > increasing.... > > A static method can't have access to class instance variables. You > could make it static, but it shouldn't reference any variables it didn't > locally declare: > > /** > * Gets a [EMAIL PROTECTED] DataSource} for database connection usage. > * @return The datasource for database connection. > * @throws SQLException > */ > public static DataSource getDataSource() throws SQLException { > Datasource ds = null ; > try { > Context initContext = new InitialContext(); > ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/myDB"); > initContext.close(); > return ds; > } catch (final NamingException e) { > e.printStackTrace(); > throw new RuntimeException("Java naming exception when > getting connection from tomcat pool: " + e.getMessage()); > } > } > > --David > > > sinoea kaabi wrote: > > Just a question here, > > I am using a Data class to retrieve the datasource > > > > public class Data { > > > > /** > > * Gets a [EMAIL PROTECTED] DataSource} for database connection usage. > > * @return The datasource for database connection. > > * @throws SQLException > > */ > > public static DataSource getDataSource() throws SQLException { > > if (ds == null) { > > DATASOURCE.info("DataSource is NULL "); > > MANY_CONNECTIONS.info("DataSource is NULL "); > > try { > > final Context initContext = new > > InitialContext(); > > ds = > > (BasicDataSource)initContext.lookup("java:/comp/env/jdbc/myDB"); > > initContext.close(); > > logDataSource(ds); > > return ds; > > } catch (final NamingException e) { > > e.printStackTrace(); > > throw new RuntimeException("Java naming > > exception when getting connection from tomcat pool: " + e.getMessage()); > > } > > } else { > > logDataSource(ds); > > return ds; > > } > > } > > > > } > > > > > > Collection branches = new BranchData().loadBranches(Data.getDataSource(), > > 1); > > > > > > Can the getDataSource method be static? > > > > Thanks, > > Sinoea > > > > > >> From: [EMAIL PROTECTED] > >> To: users@tomcat.apache.org > >> Subject: Re: Tomcat 5.5, JNDI Connection Pooling, Active connections keep > >> increasing.... > >> Date: Wed, 17 Sep 2008 12:25:17 +0200 > >> > >> > >> ----- Original Message ----- > >> From: "sinoea kaabi" > >> To: "Tomcat Users List" > >> Sent: Wednesday, September 17, 2008 11:31 AM > >> Subject: RE: Tomcat 5.5, JNDI Connection Pooling, Active connections keep > >> increasing.... > >> > >> > >> > >> Thanks, > >> First I will try to close resources before returning. > >> > >> Although I am sure that the finally blocks are reached, since I use logging > >> in the last finally block and the log is > >> outputted. > >> > >> try { > >> > >> } finally { > >> connection.close(); > >> Data.logConnection(connection); // this is logged > >> } > >> > >> But I'll give it a try anyway. > >> > >> Object object = null; > >> try { > >> > >> } finally { > >> close resources.. > >> } > >> return object; > >> > >> > >> The static methods are not thread-safe you say! > >> > >> So, what exactly does it mean when we say that Tomcat is thread safe for > >> requests. > >> Tomcat creates a new thread for each request, so somehow my static methods > >> are then thread safe (incdirectly, since it is managed by Tomcat). > >> > >> Request A> new Thread A> using my static method for loadBranches(...) > >> Request B> new Thread B> using my static method for loadBranches(...) > >> > >> Thread B must wait until Thread A is done. > >> > >> Since threads are managed by tomcat, no thread should be able to use a > >> static method that is used by another thread. > >> > >> Or in fact, you must be right, should I declare them synchronized? > >> > >> public static synchronized loadBranches(...) > >> > >> Thanks, > >> Sinoea > >> > >> ===================================== > >> > >> Yes your finally blocks are working.... I checked that... the book is right > >> ;) > >> > >> > >> On threading..... No.... just make the class *non* static.... > >> > >> Collection branches = *NEW* > >> BranchData().loadBranches(Data.getDataSource(), 1); > >> > >> so now each thread has its own class..... > >> > >> I imagine thats tomcats pool is already thread safe... > >> > >> synchronized will work... but its a bottle neck... it will make tomcat Q... > >> and there is a setting in tomcat somewhere where one can make it single > >> threaded... but again it will slow it down... you only use that when a > >> coder > >> has cocked up ;) > >> > >> In threading the thing to watch is those global variables... so without > >> seeing your actual code its difficult to spot problems... > >> Thread safety is more of an art than a science... > >> > >> New should do it because every thread is getting its own class... so a > >> class > >> is isolated, the connection and everything else is inside it... and they > >> cant mess with each other... in theory... if there are no shared globals... > >> > >> So I think whats happening in your static class is something like this... > >> > >> Thread 1 opens connection 1.... inside class > >> Thread 2 open connection 2.... also inside class > >> > >> Close connection 2 > >> Close connection 2 > >> > >> ie same connection is no closed twice and connection 1 slips out... that > >> wont happen if the class in not static... > >> > >> .... I think ;) > >> > >> have fun... > >> --------------------------------------------------------------------------- > >> HARBOR : http://www.kewlstuff.co.za/index.htm > >> The most powerful application server on earth. > >> The only real POJO Application Server. > >> See it in Action : http://www.kewlstuff.co.za/cd_tut_swf/whatisejb1.htm > >> --------------------------------------------------------------------------- > >> > >> > >> --------------------------------------------------------------------- > >> To start a new topic, e-mail: users@tomcat.apache.org > >> To unsubscribe, e-mail: [EMAIL PROTECTED] > >> For additional commands, e-mail: [EMAIL PROTECTED] > >> > >> > > > > _________________________________________________________________ > > Win New York holidays with Kellogg’s & Live Search > > http://clk.atdmt.com/UKM/go/111354033/direct/01/ > > --------------------------------------------------------------------- > > To start a new topic, e-mail: users@tomcat.apache.org > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > --------------------------------------------------------------------- > To start a new topic, e-mail: users@tomcat.apache.org > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] >
_________________________________________________________________ Make a mini you and download it into Windows Live Messenger http://clk.atdmt.com/UKM/go/111354029/direct/01/