Debian Codegraph @

src/java/org/apache/commons/dbcp/package.html

raw
1 <!--
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 -->
17 <!-- $Id: package.html 883890 2009-11-24 21:45:27Z sebb $ -->
18 <html>
19 <head>
20 <title>Package Documentation for org.apache.commons.dbcp</title>
21 </head>
22 <body>
23 <p>
24 Database Connection Pool API.
25 </p>
26
27 <b>Overview in Dialog Form</b>
28 <p>
29 Q: How do I use the DBCP package?
30 </p><p>
31 A: There are two primary ways to access the DBCP pool, as a
32 {@link java.sql.Driver Driver}, or as a {@link javax.sql.DataSource DataSource}.
33 You'll want to create an instance of {@link org.apache.commons.dbcp.PoolingDriver} or
34 {@link org.apache.commons.dbcp.PoolingDataSource}. When using one of these
35 interfaces, you can just use your JDBC objects the way you normally would.
36 Closing a {@link java.sql.Connection} will simply return it to its pool.
37 </p>
38 <p>
39 Q: But {@link org.apache.commons.dbcp.PoolingDriver PoolingDriver} and
40 {@link org.apache.commons.dbcp.PoolingDataSource PoolingDataSource} both expect an
41 {@link org.apache.commons.pool.ObjectPool ObjectPool} as an input. Where do I
42 get one of those?
43 </p><p>
44 A: The {@link org.apache.commons.pool.ObjectPool ObjectPool} interface is defined
45 in the {@link org.apache.commons.pool} package (Commons-Pool).
46 The {@link org.apache.commons.pool.impl} package has a couple of implementations,
47 and you can always create your own.
48 </p>
49 <p>
50 Q: Ok, I've found an {@link org.apache.commons.pool.ObjectPool ObjectPool}
51 implementation that I think suits my connection pooling needs. But it wants
52 a {@link org.apache.commons.pool.PoolableObjectFactory PoolableObjectFactory}.
53 What should I use for that?
54 </p><p>
55 A: The DBCP package provides a class for this purpose. It's called
56 {@link org.apache.commons.dbcp.PoolableConnectionFactory}.
57 It implements the factory and lifecycle methods of
58 {@link org.apache.commons.pool.PoolableObjectFactory}
59 for {@link java.sql.Connection}s. But it doesn't create the actual database
60 {@link java.sql.Connection}s itself, it uses a
61 {@link org.apache.commons.dbcp.ConnectionFactory} for that.
62 The {@link org.apache.commons.dbcp.PoolableConnectionFactory} will take
63 {@link java.sql.Connection}s created by the {@link org.apache.commons.dbcp.ConnectionFactory}
64 and wrap them with classes that implement the pooling behaviour.
65 </p><p>
66 Several implementations of {@link org.apache.commons.dbcp.ConnectionFactory} are
67 provided--one that uses {@link java.sql.DriverManager} to create connections
68 ({@link org.apache.commons.dbcp.DriverManagerConnectionFactory}),
69 one that uses a {@link java.sql.Driver} to create connections
70 ({@link org.apache.commons.dbcp.DriverConnectionFactory}),
71 one that uses a {@link javax.sql.DataSource} to create connections
72 ({@link org.apache.commons.dbcp.DataSourceConnectionFactory}).
73 </p>
74 <p>
75 Q: I think I'm starting to get it, but can you walk me though it again?
76 </p><p>
77 A: Sure. Let's assume you want to create a {@link javax.sql.DataSource}
78 that pools {@link java.sql.Connection}s. Let's also assume that that
79 those pooled {@link java.sql.Connection}s should be obtained from
80 the {@link java.sql.DriverManager}.
81 You'll want to create a {@link org.apache.commons.dbcp.PoolingDataSource}.
82 </p><p>
83 The {@link org.apache.commons.dbcp.PoolingDataSource} uses an underlying
84 {@link org.apache.commons.pool.ObjectPool} to create and store its
85 {@link java.sql.Connection}.
86 </p><p>
87 To create a {@link org.apache.commons.pool.ObjectPool}, you'll need
88 a {@link org.apache.commons.pool.PoolableObjectFactory} that creates
89 the actual {@link java.sql.Connection}s. That's what
90 {@link org.apache.commons.dbcp.PoolableConnectionFactory} is for.
91 </p><p>
92 To create the {@link org.apache.commons.dbcp.PoolableConnectionFactory},
93 you'll need at least two things:
94 <ol>
95 <li>
96 A {@link org.apache.commons.dbcp.ConnectionFactory} from which
97 the actual database {@link java.sql.Connection}s will be obtained.
98 </li>
99 <li>
100 An empty and factory-less {@link org.apache.commons.pool.ObjectPool}
101 in which the {@link java.sql.Connection}s will be stored.
102 <br></br>
103 When you pass an {@link org.apache.commons.pool.ObjectPool} into the
104 {@link org.apache.commons.dbcp.PoolableConnectionFactory}, it will
105 automatically register itself as the {@link org.apache.commons.pool.PoolableObjectFactory}
106 for that pool.
107 </li>
108 </ol>
109 You can optionally provide a {@link org.apache.commons.pool.KeyedObjectPoolFactory}
110 that will be used to create {@link org.apache.commons.pool.KeyedObjectPool}s for
111 pooling {@link java.sql.PreparedStatement}s for each {@link java.sql.Connection}.
112 </p><p>
113 In code, that might look like this:
114 <pre>GenericObjectPool connectionPool = new GenericObjectPool(null);
115 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");
116 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
117 PoolingDataSource dataSource = new PoolingDataSource(connectionPool);</pre>
118 </p><p>
119 To create a {@link org.apache.commons.dbcp.PoolingDriver}, we do the same thing,
120 except that instead of creating a {@link javax.sql.DataSource} on the last line,
121 we create a {@link org.apache.commons.dbcp.PoolingDriver}, and register the
122 <tt>connectionPool</tt> with it. E.g.,:
123 <pre>GenericObjectPool connectionPool = new GenericObjectPool(null);
124 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");
125 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
126 PoolingDriver driver = new PoolingDriver();
127 driver.registerPool("example",connectionPool);</pre>
128 </p><p>
129 Since the {@link org.apache.commons.dbcp.PoolingDriver} registers itself
130 with the {@link java.sql.DriverManager} when it is created, now you can just
131 go to the {@link java.sql.DriverManager} to create your {@link java.sql.Connection}s,
132 like you normally would:
133 <pre>Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");</pre>
134 </p>
135 <p>
136 Q: Sounds complicated, is there an easier way?
137 </p><p>
138 A: If you're using the {@link org.apache.commons.dbcp.PoolingDriver}, you
139 don't need to do this configuration in code. Instead, you can provide
140 a JOCL document that describes the connection pool,
141 and let the {@link org.apache.commons.dbcp.PoolingDriver} discover it at
142 runtime.
143 </p><p>
144 Specifically, if the {@link org.apache.commons.dbcp.PoolingDriver} is asked for
145 a {@link java.sql.Connection} from a pool that has not yet been registered,
146 it will look for a named resource from which to read the pool's configuration,
147 and create that pool.
148 </p><p>
149 For example, suppose you create a pool named "<tt>/eg</tt>" from a JOCL
150 document. The "connect string" for this pool will be
151 "<tt>jdbc:apache:commons:dbcp:/eg</tt>". To do this, you'll need a create
152 a <i>resource</i> (just a file in your classpath) containing a JOCL description
153 of the pool. Specifically, this JOCL document should define a
154 {@link org.apache.commons.dbcp.PoolableConnectionFactory} from which the
155 pool will be obtained. For example:
156 <pre>&lt;object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl"&gt;
157 &lt;!-- the first argument is the ConnectionFactory --&gt;
158 &lt;object class="org.apache.commons.dbcp.DriverManagerConnectionFactory"&gt;
159 &lt;string value="jdbc:some:connect:string"/&gt;
160 &lt;object class="java.util.Properties" null="true"/&gt;
161 &lt;/object&gt;
162 &lt;!-- the next argument is the ObjectPool --&gt;
163 &lt;object class="org.apache.commons.pool.impl.GenericObjectPool"&gt;
164 &lt;object class="org.apache.commons.pool.PoolableObjectFactory" null="true"/&gt;
165 &lt;int value="10"/&gt; &lt;!-- max active --&gt;
166 &lt;byte value="1"/&gt; &lt;!-- when exhausted action, 0 = fail, 1 = block, 2 = grow --&gt;
167 &lt;long value="2000"/&gt; &lt;!-- max wait --&gt;
168 &lt;int value="10"/&gt; &lt;!-- max idle --&gt;
169 &lt;boolean value="false"/&gt; &lt;!-- test on borrow --&gt;
170 &lt;boolean value="false"/&gt; &lt;!-- test on return --&gt;
171 &lt;long value="10000"/&gt; &lt;!-- time between eviction runs --&gt;
172 &lt;int value="5"/&gt; &lt;!-- number of connections to test per eviction run --&gt;
173 &lt;long value="5000"/&gt; &lt;!-- min evictable idle time --&gt;
174 &lt;boolean value="true"/&gt; &lt;!-- test while idle --&gt;
175 &lt;/object&gt;
176 &lt;!-- the next argument is the KeyedObjectPoolFactory --&gt;
177 &lt;object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory"&gt;
178 &lt;int value="5"/&gt; &lt;!-- max idle --&gt;
179 &lt;/object&gt;
180 &lt;string value="SELECT COUNT(*) FROM DUAL"/&gt; &lt;!-- validation query --&gt;
181 &lt;boolean value="false"/&gt; &lt;!-- default read only --&gt;
182 &lt;boolean value="true"/&gt; &lt;!-- default auto commit --&gt;
183 &lt;/object&gt;</pre>
184 </p><p>
185 Simply save that file somewhere in your classpath as <tt>eg.jocl</tt>,
186 and the {@link org.apache.commons.dbcp.PoolingDriver} will find it
187 automatically. You need only register the {@link org.apache.commons.dbcp.PoolingDriver}
188 (for example, using the <tt>jdbc.drivers</tt> property), and use the
189 the {@link java.sql.DriverManager} to create your {@link java.sql.Connection}s,
190 like you normally would:
191 <pre>Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/eg");</pre>
192 </p><p>
193 (Note that without the leading slash, the pool must be located at
194 <tt>org/apache/commons/dbcp/PoolingDriver/eg.jocl</tt> within your classpath.
195 See {@link java.lang.Class#getResource} for details.)
196 </p>
197
198 </body>
199 </html>
200
201