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 .. 131 132 133 134 135 136 < 137 > 138 139 140 141 142 143 .. 166 >> Next

Item 45: Take a Dip in the Resource Pool
Most of the time, an "interactive Web site" is really a controlled chaos of Web applications that gradually evolve into a common purpose. Most organizations come up with an idea that "we can put these applications on the Web," and they usually do. Depending on the skill level of the development organization, various technologies are used to "Web-enable" their application.
This phenomenon is not limited to the spread of Web application platforms avail-able—PHP, Cold Fusion, ASP, and so on. This occurs within Java also. The power of servlets, the ease of JSP, and the reuse of JSP tag libraries have caused a lot of developers to use a wide variety of techniques to Web-enable applications in Java.
Here are a few common ways that Web developers handle database connections. The first is to create a simple class to abstract the DB from the rest of the code, as Listing 45.1 demonstrates.
01 package org.javapitfalls;
02
03 import java.io.*;
04 import java.net.*;
05 import java.sql.*;
06 import java.util.*;
07
08 public class DbAbstract implements java.io.Serializable {
09
10 private Connection connection;
Listing 45.1 DBAbstract.java
Take a Dip in the Resource Pool 379
11 private Statement statement;
12
13 public DbAbstract ()
14 throws ClassNotFoundException, SQLException
15 {
16
17 String driverName = "";
18 String className = "";
19 String user = "";
20 String pass = "";
21
22 Properties resource = new Properties();
23 // Obtain a resource bundle that will allow use to get the
24 // appropriate configurations for the server.
25 try
26 {
27 URL url = 2
this.getClass().getClassLoader().getResource("db.properties");
28 resource.load( new FileInputStream(url.getFile()) );
29 // Get properties
30 driverName = resource.getProperty("driverName" );
31 className = resource.getProperty("className");
32 user = resource.getProperty("user");
33 pass = resource.getProperty("pass");
34 System.out.println("Using parameters from the db.properties
file for Database.");
35 } catch (Exception e) {
36 System.out.println("ERROR: Couldn't load db .properties." 2
+ e.toString());
37 }
38
39 Class.forName(className);
40 connection = DriverManager.getConnection(driverName,user, pass);
41
42 connection.setAutoCommit(false);
43
44 statement = connection.createStatement();
45 }
46
47 public void executeUpdate(String sqlCommand)
48 throws SQLException
49 {
50 statement.executeUpdate(sqlCommand);
51 }
52
53 public ResultSet executeQuery(String sqlCommand)
54 throws SQLException
55 {
56 return statement.executeQuery(sqlCommand);
Listing 45.1 (continued)
380 Item 45
57 }
58
59 public void commit() throws SQLException
60 {
61 connection.commit();
62 }
63
64 public void rollback() throws SQLException
65 {
66 connection.rollback();
67 }
68
69 protected void finalize() throws SQLException
70 {
71 statement.close();
72 connection.close();
73 }
74 }
75
Listing 45.1 (continued)
This example works very well. Taking it one step further, we find the development of servlets to pool database connections or broker database connections. I have seen a number of innovative approaches to creating these types of servlets, including HTTP interfaces to get state information, manipulate the interface, or control lifecycle events. Because I don't want to take anything away from these approaches or distract from my main point, I've just provided a simple example of one of these in Listing 45.2.
01 package org.java.pitfalls;
02
03 import javax.servlet.*;
04 import javax.servlet.http.*;
05 import java.io.*;
06 import java.util.*;
07 import java.sql.*;
08 import java.text.*;
09 import com.javaexchange.dbConnectionBroker.*;
10
11 public class CPoolServlet extends HttpServlet
12 implements SingleThreadModel {
13
14
15 ServletContext context = null;
16 DbConnectionBroker broker = null;
Listing 45.2 CPoolServlet.java
Take a Dip in the Resource Pool 381
17: String logDir = "";
18:
19: public void init(ServletConfig config) {
20:
21: try {
22:
23: startBroker();
24:
25: // Set it up so that other servlets can get a handle to ^
it.
26: context = config.getServletContext();
27: context.setAttribute("pooler", broker);
28:
29: }catch (IOException e) {
30: System.out.println(
31: "CPoolServlet: Problems with database connection: " + ^
e);
32:
33: }catch (Exception e) {
34: System.out.println(
35: "CPoolServlet: General pooler error: " + e);
36: }
37: }
38:
39: public void doPost(HttpServletRequest request, ^
HttpServletResponse response)
40: throws IOException {
41: doGet(request, response);
42: }
43:
44: public void doGet(HttpServletRequest request, ^
HttpServletResponse response)
45
46
47
48
49
50
51
52
53
54
55
Information</h2></center>");
56: html.append("<p><center>Connection Pool is available ^
Previous << 1 .. 131 132 133 134 135 136 < 137 > 138 139 140 141 142 143 .. 166 >> Next