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

01 package org.javapitfalls.item32;
02
03 import com.devdaily.opensource.database.DDConnectionBroker;
04 import java.io.*;
05 import java.sql.*;
06
07 /**
08 /* This is our class that will be shared across all of the
09 * servlets for the ’Lavender’ database. It is a singleton,
10 * and also works as an adapter to the connection broker
11 * class that we are using.
12 */
13 public class LavenderDBSingleton
14 {
15
16 private DDConnectionBroker m_broker;
17 private static LavenderDBSingleton m_singleton = null;
18
19 private LavenderDBSingleton()
20 {
21 /*
22 * We will put all of our database-specific information
23 * here. Please note that we could have read this
24 * information from a properties file.
25 */
26
27 String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
28 String url = "jdbc:odbc:Lavender";
29 String uname = "";
30 String passwd = "";
Listing 32.3 Singleton class for sharing a connection pool
'Gamma, Helm, Johnson, Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. 1995. Reading, Mass.: Addison-Wesley.
Design Flaws with Creating Database Connections within Servlets 287
31
32 int minConnections = 1;
33 int maxConnections = 10;
34 long timeout = 100;
35 long leaseTime = 60000;
36 String logFile = "c:/tmp/ConnectionPool.log";
37
38 try
39 {
40 m_broker = new DDConnectionBroker(driver,
41 url, uname, passwd,
42 minConnections,
43 maxConnections,
44 timeout,
45 leaseTime,
46 logFile);
47 }
48 catch (SQLException se)
49 {
50 System.err.println( se.getMessage() );
51 }
52 }
53 /**
54 * getInstance() returns the class, instantiating it
55 * if there is not yet an instance in the VM.
56 */
57 public synchronized static LavenderDBSingleton getInstance()
58 {
59 if (m_singleton == null)
60 {
61 m_singleton = new LavenderDBSingleton();
62 }
63
64 return (m_singleton);
65 }
66
67 /*
68 * calls getConnection() on the broker class
69 */
70 public synchronized Connection getConnection() throws Exception
71 {
72 if (m_broker == null)
73 {
74 throw new Exception("Can't get Connection broker!");
75 }
76 return (m_broker.getConnection());
77 }
78
79 /*
Listing 32.3 (continued)
288 Item 32
80 * frees the connection from the broker class
81 */
82 public synchronized void freeConnection(Connection con)
83 throws Exception
84 {
85 if (m_broker == null )
86 {
87 throw new Exception("Can't get Connection broker!");
88 }
89 m_broker.freeConnection(con);
90 }
91 }
92
93
Listing 32.3 (continued)
Listing 32.4 shows the final version of our servlet with our Singleton class in action. In the init() method, we get the instance of our Singleton class on line 23. On line 72, we call the getConnection() method of our Singleton, and finally, on line 107, we free the connection. If the other servlets in our example use the Singleton that does connection pooling and database connection management, we will maximize the efficiency of our servlet, reducing the overhead of creating connections for every client.
001 package org.javapitfalls.item32;
002
003 import java.io.*;
004 import java.sql.*;
005 import java.text.*;
006 import java.util.*;
007 import javax.servlet.*;
008 import javax.servlet.http.*;
009
010 public class BestQueryServlet extends HttpServlet
011 {
012 //only set in the init() method, so concurrency
013 //issues should be fine.
014 private LavenderDBSingleton m_dbsingleton = null;
015
016 public void init()
017 {
018 /*
019 * This will instantiate it within the Servlet's
020 * virtual machine if it hasn't already. If it
021 * has, we have the instance of it.
022 */
023 m_dbsingleton = LavenderDBSingleton.getInstance();
Listing 32.4 Servlet Sharing Connection Pool Across Server
Design Flaws with Creating Database Connections within Servlets 289
024 }
025 /**
026 * simply forwards all to doPost()
027 */
028 public void doGet(HttpServletRequest request,
029 HttpServletResponse response)
030 throws IOException, ServletException
031 {
032 doPost(request,response);
033 }
034
035 /**
036 * The main form!
037 */
038 public void doPost(HttpServletRequest request,
039 HttpServletResponse response)
040 throws IOException, ServletException
041 {
042 PrintWriter out = response.getWriter();
043
044 out.println("<TITLE>Internal Inventory Check</TITLE>");
045 out.println("<BODY BGCOLOR=’white’>");
046 out.println("<H1>Lavender Fields Farm Internal Inventory</H1>");
047
048 //show the date.
049 SimpleDateFormat sdf =
050 new SimpleDateFormat ("EEE, MMM d, yyyy h:mm a");
051 java.util.Date newdate =
052 new java.util.Date(
053 Calendar.getInstance().getTime().getTime()
054 );
055 String datestring = sdf.format(newdate);
056
057 out.println("<H3>Inventory as of: " + datestring + "</H3>");
058
059 out.println("<TABLE BORDER=1>");
060 out.println("<TR><TD BGCOLOR=’yellow’>" +
061 "<B><CENTER>Name</CENTER></B></TD>" +
Previous << 1 .. 99 100 101 102 103 104 < 105 > 106 107 108 109 110 111 .. 166 >> Next