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

under the pooler attribute.</center>");
57:
58: html.append("</body></html>");
59:
throws IOException {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
StringBuffer html = new StringBuffer();
html.append("");
html.append("<html><title>CPoolServlet</title><body>"); html.append("<center><h2>CPoolServlet ^
Listing 45.2 (continued)
382 Item 45
60 out.println(html.toString());
61 }
62
63 private void startBroker() throws Exception {
64 try {
65 broker = new DbConnectionBroker(
66 "com.sybase.jdbc2.jdbc.SybDriver",
67 "jdbc:sybase:Tds:localhost:2 638",
68 "Test",
69 "Test",
70 5,
71 25,
72 "CPool.log",
73 1.0);
74 }catch (Exception e) {
75 throw e;
76 }
77 }
78
79 public void destroy() {
80 broker.destroy();
81 }
82 }
83
Listing 45.2 (continued)
In this example, the servlet creates a connection broker object, in this case, a popular one from the Java Exchange. It places the attribute within the servlet context so that other components (servlets, JSPs) within the container can use it. This shows that there is an ability to create objects and make them available to the rest of the Web container. Why can't the container create these objects and make them available through the same JNDI lookups? It can. In fact, J2EE 1.3 containers are required to support resource pools, which are called resource managers.
Another interesting thing about the specification is that application developers are required to get their database connections through the javax.sql.DataSource connection factory. That means that to be compliant with the specification, developers ought to use this facility.
As an example, let's look at how you would do this using Tomcat. In this case, we are using an open-source connection pool factory provided by the Jakarta Commons project called DBCP (http://jakarta.apache.org/commons/dbcp.html). Assuming you have placed your JDBC driver and the connection pool JARs (if needed) in the right place (%CATALINA_HOME%/common/lib for Tomcat), the first step is to place a resource reference in your Web application's web.xml file—as demonstrated in Listing 45.3.
Take a Dip in the Resource Pool 383
01 <resource-ref>
03 <description>
05 Reference to a factory for java.sql.Connection instances. 2
The actual implementation is configured in the container.
07 </description>
09 <res-ref-name>
11 jdbc/myDB
13 </res-ref-name>
15 <res-type>
17 javax.sql.DataSource
19 </res-type>
21 <res-auth>
23 Container
25 </res-auth>
27 </resource-ref>
Listing 45.3 Excerpt from web.xml
As noted in the above declaration, you then need to configure the container to handle the actual implementation of the reference. In Tomcat, this is done in the server.xml file. Here is an example of what should be added to the file in order to create the resource pool.
Note this fits into the Context element, or in the DefaultContext element. More information on these can be read from the sample server.xml and other documents that the Jakarta project maintains on Tomcat. Listing 45.4 shows how to modify the server.xml in Tomcat to create the Connection Pool resource.
01 <Context ... >
03
04
05 <Resource name="jdbc/myDB" auth="Container"
06 type="javax.sql.DataSource"
07 description="The PKO Portal DB"/>
08 <ResourceParams name="jdbc/pkoDB">
09 <parameter>
10 <name>factory</name>
11 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
12 </parameter>
13 <parameter>
14 <name>driverClassName</name>
15 <value>com.sybase.jdbc2.jdbc.SybDriver</value>
16 </parameter>
17 <parameter>
Listing 45.4 Excerpt from server.xml (continued)
384 Item 45
18 <name>maxActive</name>
19 <value>3</value>
20 </parameter>
21 <parameter>
22 <name>maxIdle</name>
23 <value>3</value>
24 </parameter>
25 <parameter>
26 < name >u s e r name< / name >
27 <value>user</value>
28 </parameter>
29 <parameter>
30 <name>pas sword</name>
31 <value>pwd</value>
32 </parameter>
33 <parameter>
34 <name>maxWait</name>
35 <value>-1</value>
36 </parameter>
37 <parameter>
38 <name>url</name>
39 <value>jdbc:sybase:Tds:localhost:2 638</value>
40 </parameter>
41 </ResourceParams>
Listing 45.4 (continued)
So, now the JDBC DataSource resource pool is available to the applications. The last step would be to actually use it. Listing 45.5 is an example of how to use the resource pool.
01 Context initCtx = new InitialContext();
03 Context envCtx = (Context) initCtx.lookup(" java:comp/env");
05 DataSource ds = (DataSource) envCtx.lookup( "jdbc/myDB");
07 Connection conn = ds.getConnection();
09 <<< Execute specific JDBC code here >>>
11 conn.close();
Listing 45.5 Using the resource pool
There are many different ways to create and manage resource pools. Like a lot of things in Java, better solutions evolve with time, and developers often are not aware of improvements offered in the platform. This is a common example where developers who have created their own mechanisms may not be aware of the improvements.
Previous << 1 .. 132 133 134 135 136 137 < 138 > 139 140 141 142 143 144 .. 166 >> Next