Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 97 98 99 100 101 102 < 103 > 104 105 106 107 108 109 .. 166 >> Next

58 ResultSet rs = stmt.executeQuery("select * from Inventory") ;
59
60 while (rs.next())
61 {
62 String amtString = "";
63 int amt = rs.getInt("Amount");
64 if (amt < 50)
65 amtString ="<TD><CENTER><FONT COLOR='RED'>" + amt +
66 "</FONT></CENTER></TD>";
67 else
68 amtString ="<TD><CENTER>" + amt + "</CENTER></TD>' • r
69
70 out.println("<TR><TD><CENTER>" + rs.getString("Name") +
71 "</CENTER></TD><TD><CENTER>" +
72 rs.getString("Description") + "</CENTER>" +
73 "</TD>" + amtString + "</TR>");
74 }
75 rs.close();
76 out.println("</TABLE><HR>Items in <FONT COLOR='red'>RED" +
77 "</FONT> denote low inventory levels. Click " +
78 "Here to Contact <A HREF='mailto:mgmt@localhost'>" +
79 "MANAGEMENT</A> to order more supplies.");
80 }
81 catch (Exception e)
82 {
83 out.println("There were errors connecting to the database." +
84 " See your systems administrator for details.") ;
85 e.printStackTrace();
86 }
87
88
89 }
90
91
92 }
Listing 32.1 (continued)
The problem is twofold: opening a new connection to the database is a computationally expensive operation, and there is a finite number of open connections that a database can have. In line 55 in Listing 32.1, we open a new connection on line 55 every time a user loads the Web page. On line 75, we close the result set, resulting in the eventual termination of the connection. When there are hundreds (or thousands) of users connecting to that online store, that strategy will simply not suffice.
282 Item 32
What is a better solution? This is where the specifics of your application impact your decision. If this servlet were the only one connecting to the database, you could design it so that the servlet shares connections with itself. That is, a set of connections could be shared, reused, recycled, and managed by this servlet. If, however, the entire Web server shared connections between servlets, you should design it so that all the servlets share the management of connections with each other.
This is where connection pooling comes into play. Connection pooling involves allocating database connections in advance, along with the reuse and management of the connections. You could write your own connection pool package that your servlet could use, but many are available on the Internet. For our next example, we used an open-source connection broker called DDConnectionBroker, from http://opensource .devdaily.com/. This package offers the basics that any connection pooling class should—including the pre-allocation, reuse, and management of database connections.
In Listing 32.2, we use the connection broker within the servlet. In our init() method, we instantiate the connection broker, specifying the details of our database connection and setting our broker to be the instance variable in line 13. Since the init() method is only called once (right after instantiation), our connection pool is initialized once. We specify that the connection pool will have a maximum number (10) of database connections. This number is dependent on the configuration of your database. After setting up the connection broker in the init() method, the only further change to the servlet is that instead of creating the connection in the doPost() method, we call the method m_broker .getConnection() in line 95 and call the method m_broker.freeConnection() in line 119. The result is that every thread going through this servlet (every user loading the Web page) will use a connection pool in getting to the database.
?' Internal Itivotitoiy Check Microsoft Internet Explotot I La
EJe fccit yiew Favorites lools Help »'
QBKk - j «1 @1 ˆD > @ " J
Lavender Fields Farm Internal Inventory
Iiiveutory as of: Tue. Sep 10. 2002 9:24 PM
Name Description Tnventoiy Amount
Shea Butter Soap Made with African Shea butter for dry or sensitive skin 200
Milk and Honey Soap Nubian goats milk for silky protein and natural honey for its antibiotic qualities Leaves even sensitive skm soil and moist. AAA
Silk Soap Truly luxurious soap with liquid silk For all skm types 542
Nourishing Facial Scrub Ground oatmeal, lavender and rosemary with powdered goats? milk and lavender essential oil Mix with a little water and gently massage over the face and throat (avoid the delicate eye area) to reveal fresh, younger looking skm 22
Lavender Mmt Hand. Foot, and Body Scrub Fine sea salt with handmade castile soap, jojoba oil and glyccrin with essential oils of peppermint and lavender. Scrubs away dead skm and debris to reveal fresher, younger looking skin 321
Bath Tea A special blend of rose petals, lavender, chamomile, lemongrass and peppermint Add tea bag to warm bath water to draw out toxins, ease away tension and soothe and soften skm. Safe for whirlpool baths. 33
Lavender Pillows Lovely small pillows and sachets filled with dned lavender buds to toss among bed pillows, in drawers or linen closets. Repels moths and other insects with a delightful fragrance that lasts for years 44
Previous << 1 .. 97 98 99 100 101 102 < 103 > 104 105 106 107 108 109 .. 166 >> Next