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 .. 98 99 100 101 102 103 < 104 > 105 106 107 108 109 110 .. 166 >> Next

Items in RED denote a possible low inventory. Click Here to Contact MANAGEMENT to order more supplies.
Done ^ Local htranet
Figure 32.1 Screen capture of inventory page.
Design Flaws with Creating Database Connections within Servlets 283
000 package org.javapitfalls.item32;
001 import com.devdaily.opensource.database.DDConnectionBroker;
002 import java.io.*;
003 import java.sql.*;
004 import java.text.*;
005 import java.util.*;
006 import javax.servlet.*;
007 import javax.servlet.http.*;
008
009 public class BetterQueryServlet extends HttpServlet
010 {
011 //only set in the init() method, so concurrency
012 //issues should be fine.
013 private DDConnectionBroker m_broker = null;
014
015 public void init()
016 {
017 String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
018 String url = "jdbc:odbc:Lavender";
019 String uname = "";
020 String passwd = "";
021
022 int minConnections = 1;
023 int maxConnections = 10;
024 long timeout = 100;
025 long leaseTime = 60000;
026 String logFile = "c:/tmp/ConnectionPool.log";
027
028 try
029 {
030 m_broker = new DDConnectionBroker(driver,
031 url, uname, passwd,
032 minConnections,
033 maxConnections,
034 timeout,
035 leaseTime,
036 logFile);
037 }
038 catch (SQLException se)
039 {
040 System.err.println( se.getMessage() );
041 }
042
043
044 }
045 /**
046 * simply forwards all to doPost()
047 */
048 public void doGet(HttpServletRequest request,
Listing 32.2 Sharing a connection within a servlet (continued)
284 Item 32
049 HttpServletResponse response)
050 throws IOException, ServletException
051 {
052 doPost(request,response);
053 }
054
055 / **
056 * The main form!
057 */
058 public void doPost(HttpServletRequest request,
059 HttpServletResponse response)
060 throws IOException, ServletException
061 {
062 Printwriter out = response.getWriter();
063
064 if (m_broker == null)
065 {
066 out.println("<B>There are currently database problems. " +
067 "Please see your administrator for details.</B>");
068 return;
069 }
070 out.println("<TITLE>Internal Inventory Check</TITLE>");
071 out.println("<BODY BGCOLOR='white'>");
072 out.println("<H1>Lavender Fields Farm Internal Inventory</H1>");
073
074 //show the date.
075 SimpleDateFormat sdf =
076 new SimpleDateFormat ("EEE, MMM d, yyyy h:mm a");
077 java.util.Date newdate =
078 new java.util.Date(Calendar.getInstance().getTime().getTime());
079 String datestring = sdf.format(newdate);
080
081 out.println("<H3>Inventory as of: " + datestring + "</H3>");
082
083 out.println("<TABLE BORDER=1>");
084 out.println("<TR><TD BGCOLOR='yellow'>" +
085 "<B><CENTER>Name</CENTER></B></TD>" +
086 "<TD BGCOLOR='yellow'><B><CENTER>Description</CENTER></B></TD>" +
087 "<TD BGCOLOR='yellow'><B><CENTER>Inventory Amount</CENTER></B>" +
088 "</TD></TR>");
089
090 //Load the inventory from the database.
091
092 try
093 {
094
095 Connection con = m_broker.getConnection();
096
097 Statement stmt = con.createStatement();
Listing 32.2 (continued)
Design Flaws with Creating Database Connections within Servlets 285
098 ResultSet rs = stmt.executeQuery("select * from Inventory );
099
100 while (rs.next())
101 {
102 String amtString = "";
103 int amt = rs.getInt("Amount");
104 if (amt < 50)
105 amtString ="<TD><CENTER><FONT COLOR='RED'>" + amt + "</FONT></CENTER></TD>"; 2
106 else
107 amtString ="<TD><CENTER>" + amt + "</CENTER></TD> ";
108 out.println("<TR><TD><CENTER>" + rs.getString("Name") +
109 "</CENTER></TD><TD><CENTER>" + rs.getString("Description") + "</CENTER> 2 +
110 "</TD>" + amtString + "</TR>");
111 }
112 rs.close();
113 out.println("</TABLE><HR>Items in <FONTCOLOR='red'>RED</FONT>" 2
114 +" denote a possible low inventory. Click Here to
115 " Contact <A HREF='mailto:mgmt@localhost'>" +
116 "MANAGEMENT</A> to order more supplies.");
117
118 //Free the connection!
119 m_broker.freeConnection( con );
120
121 }
122 catch (Exception e)
123 {
124 out.println("There were errors connecting to the database.
125 "See your systems administrator for details." );
126 e.printStackTrace();
127 }
128
129 }
130
131
132 }
Listing 32.2 (continued)
The effect of Listing 32.2 is that every user that runs the servlet BetterQueryServlet will share the connection broker object, so that connections will be shared and reused. Even better, our DDConnectionBroker object is instantiated in the init() method, where it pre-allocates connections before they are requested. This will make performance of this servlet better, and it is a good method of database connection management when that servlet is the only application talking to your database.
286 Item 32
In our scenario, however, we said that customers at our online store will be connecting to the database as well. This means that it will be wise to share the database connections across all servlets. How could we do this? One of the best ways to do this is to use the Gang of Four's Singleton design pattern.1 A very convenient design pattern, a Singleton is used when there should be only one instance of a class in a virtual machine. In our scenario, it would be great to get an instance of a connection broker from any servlet and be able to use one of the connections. Listing 32.3 shows a simple Singleton that also acts as an adapter to a few methods of the connection broker. This class, LavenderDBSingleton, will be our single point of entry to the database for our "Lavender Fields Farm" example. In our private constructor in lines 17 to 50, we instantiate our connection pool. In our getInstance() method in lines 55 to 63, you can see that this class will only be instantiated once. Finally, freeConnection() and getConnection() are simply wrappers to the methods in the DDConnection-Broker class.
Previous << 1 .. 98 99 100 101 102 103 < 104 > 105 106 107 108 109 110 .. 166 >> Next