|
Thanks Joe for pointing out the thread safety issue.Let servlet manage a connection is only for light traffic applications as I mentioned. In this case one should synchronize the appropriate block for thread safety.
Joe Sam Shirah wrote:
Hi Bruce, If I understand your response correctly, that is, using one connection for all instances of a servlet, you could run into some serious problems. The primary one is that commit() and rollback() are Connection methods. So, even if only two users share the connection, an unexpected commit or rollback could occur. The other thing I can think of off the top of my head is that while compliant drivers are required to handle multiple Statements on a single Connection, some do so by handling them sequentially, causing really bad response time. I believe the JTOpen driver does it properly but you should always be careful. To Ron: The others are correct that you should use a DataSource and pooling in a multithreaded context. Some would say use them all the time. Normally I would point you to some articles, but I'm in a rush today. It's well worth your time to google, read and understandsome articles on the benefits of DataSources and pooling. Joe Sam Joe Sam Shirah - http://www.conceptgo.com conceptGO - Consulting/Development/Outsourcing Java Filter Forum: http://www.ibm.com/developerworks/java/ Just the JDBC FAQs: http://www.jguru.com/faq/JDBC Going International? http://www.jguru.com/faq/I18N Que Java400? http://www.jguru.com/faq/Java400----- Original Message ----- From: "Bruce Jin" <brucej@xxxxxxxxxxxxxxxxxxxx>To: "Java Programming on and around the iSeries / AS400" <java400-l@xxxxxxxxxxxx> Sent: Friday, March 24, 2006 10:34 AM Subject: Re: AS/400 JDBC ConnectionIf you only have a few servlets and the traffic is not that heavy you can open/close connection in your servlet. However you should open connection in init method and close it in destroy method so that they are done only once in the life time of the servlet. Make sure the method signatures are right so that they really override the ones in super class. If you have many servlets and have heavy traffic it is a good idea to setup a connection pool. RPower@xxxxxxxxxx wrote:I'm trying to figure out if I should be creating and closing my connections to the JDBC each time a servlet is called or should I just open it once and leave it open? ie: public void performTask(HttpServletRequest req, HttpServletResponse resp) { sess = req.getSession(true); customer = (Customer) sess.getAttribute("customer"); createAuditTrail = false; try { //!---New code put in Jun 2, 2005 if (ds == null) getDS(); conn = ds.getConnection(); dmd = conn.getMetaData(); code here..... conn.close(); is this efficient or am I wasting valuable resources? Thanks, Ron Power Programmer Information Services City Of St. John's, NL P.O. Box 908 St. John's, NL A1C 5M2 709-576-8132 rpower@xxxxxxxxxx http://www.stjohns.ca/
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.