Source Code Cross Referenced for JDBCPoolComponent.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » component » jdbcpool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » ow2 easybeans » org.ow2.easybeans.component.jdbcpool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006-2007 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: JDBCPoolComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.component.jdbcpool;
025:
026:        import javax.naming.InitialContext;
027:        import javax.naming.NamingException;
028:
029:        import org.ow2.easybeans.component.api.EZBComponent;
030:        import org.ow2.easybeans.component.api.EZBComponentException;
031:        import org.ow2.easybeans.transaction.JTransactionManager;
032:        import org.ow2.util.log.Log;
033:        import org.ow2.util.log.LogFactory;
034:
035:        /**
036:         * Defines a component that creates a JDBC pool in order to use it in EasyBeans.
037:         * @author Florent Benoit
038:         */
039:        public class JDBCPoolComponent implements  EZBComponent {
040:
041:            /**
042:             * Logger.
043:             */
044:            private static Log logger = LogFactory
045:                    .getLog(JDBCPoolComponent.class);
046:
047:            /**
048:             * Default username.
049:             */
050:            private static final String DEFAULT_USER = "";
051:
052:            /**
053:             * Default password.
054:             */
055:            private static final String DEFAULT_PASSWORD = "";
056:
057:            /**
058:             * Default min pool.
059:             */
060:            private static final int DEFAULT_MIN_POOL = 10;
061:
062:            /**
063:             * Default max pool.
064:             */
065:            private static final int DEFAULT_MAX_POOL = 30;
066:
067:            /**
068:             * Default prepared statement.
069:             */
070:            private static final int DEFAULT_PSTMT = 10;
071:
072:            /**
073:             * Default prepared statement.
074:             */
075:            private static final int DEFAULT_CHECK_LEVEL = 0;
076:
077:            /**
078:             * Level of checking on connections when got from the pool. this avoids
079:             * reusing bad connections because too old, for example when database was
080:             * restarted... 0 = no checking 1 = check that still physically opened. 2 =
081:             * try a null statement.
082:             */
083:            private int checkLevel = DEFAULT_CHECK_LEVEL;
084:
085:            /**
086:             * Connection manager object.
087:             */
088:            private ConnectionManager connectionManager = null;
089:
090:            /**
091:             * JNDI name.
092:             */
093:            private String jndiName = null;
094:
095:            /**
096:             * Username.
097:             */
098:            private String username = DEFAULT_USER;
099:
100:            /**
101:             * Password.
102:             */
103:            private String password = DEFAULT_PASSWORD;
104:
105:            /**
106:             * URL for accessing to the database.
107:             */
108:            private String url = null;
109:
110:            /**
111:             * Name of the driver class to use.
112:             */
113:            private String driver = null;
114:
115:            /**
116:             * Use transaction or not ?
117:             */
118:            private boolean useTM = true;
119:
120:            /**
121:             * Pool min.
122:             */
123:            private int poolMin = DEFAULT_MIN_POOL;
124:
125:            /**
126:             * Pool max.
127:             */
128:            private int poolMax = DEFAULT_MAX_POOL;
129:
130:            /**
131:             * Max of prepared statement.
132:             */
133:            private int pstmtMax = DEFAULT_PSTMT;
134:
135:            /**
136:             * Default constructor.
137:             */
138:            public JDBCPoolComponent() {
139:                connectionManager = new ConnectionManager();
140:                connectionManager.setTransactionIsolation("default");
141:            }
142:
143:            /**
144:             * Init method.<br/> This method is called before the start method.
145:             * @throws EZBComponentException if the initialization has failed.
146:             */
147:            public void init() throws EZBComponentException {
148:                // Check that data are correct
149:                validate();
150:
151:                connectionManager.setDatasourceName(jndiName);
152:                connectionManager.setDSName(jndiName);
153:                connectionManager.setUrl(url);
154:                try {
155:                    connectionManager.setClassName(driver);
156:                } catch (ClassNotFoundException e) {
157:                    throw new IllegalStateException("Cannot load jdbc driver '"
158:                            + driver + "'.", e);
159:                }
160:                connectionManager.setUserName(username);
161:                connectionManager.setPassword(password);
162:                connectionManager.setTransactionIsolation("default");
163:                connectionManager.setPstmtMax(pstmtMax);
164:                connectionManager.setCheckLevel(checkLevel);
165:
166:            }
167:
168:            /**
169:             * Validate current data.
170:             * @throws EZBComponentException if validation fails.
171:             */
172:            private void validate() throws EZBComponentException {
173:                // check that there is a JNDI name
174:                if (jndiName == null) {
175:                    throw new EZBComponentException("No JNDI name set");
176:                }
177:
178:                // check that there is an URL
179:                if (url == null) {
180:                    throw new EZBComponentException("No URL set");
181:                }
182:
183:                // Check that there is a driver classname
184:                if (driver == null) {
185:                    throw new EZBComponentException("No driver set");
186:                }
187:            }
188:
189:            /**
190:             * Start method.<br/> This method is called after the init method.
191:             * @throws EZBComponentException if the start has failed.
192:             */
193:            public void start() throws EZBComponentException {
194:                // set settings
195:                if (useTM) {
196:                    connectionManager.setTm(JTransactionManager
197:                            .getTransactionManager());
198:                }
199:                connectionManager.setPoolMin(poolMin);
200:                connectionManager.setPoolMax(poolMax);
201:
202:                // Something is there ?
203:                try {
204:                    Object o = new InitialContext().lookup(jndiName);
205:                    if (o != null) {
206:                        logger.warn("Entry with JNDI name {0} already exist",
207:                                jndiName);
208:                    }
209:                } catch (NamingException e) {
210:                    logger.debug("Nothing with JNDI name {0}", jndiName);
211:                }
212:
213:                // Bind the resource.
214:                try {
215:                    new InitialContext().rebind(jndiName, connectionManager);
216:                } catch (NamingException e) {
217:                    throw new EZBComponentException(
218:                            "Cannot bind a JDBC Datasource with the jndi name '"
219:                                    + jndiName + "'.");
220:                }
221:
222:                logger.info("DS ''{0}'', URL ''{1}'', Driver = ''{2}''.",
223:                        jndiName, url, driver);
224:            }
225:
226:            /**
227:             * Stop method.<br/> This method is called when component needs to be
228:             * stopped.
229:             * @throws EZBComponentException if the stop is failing.
230:             */
231:            public void stop() throws EZBComponentException {
232:                // Unbind the resource.
233:                try {
234:                    new InitialContext().unbind(jndiName);
235:                } catch (NamingException e) {
236:                    throw new EZBComponentException(
237:                            "Cannot unbind a JDBC Datasource with the jndi name '"
238:                                    + jndiName + "'.");
239:                }
240:            }
241:
242:            /**
243:             * Sets the name of the JDBC driver.
244:             * @param driver the driver's name
245:             */
246:            public void setDriver(final String driver) {
247:                this .driver = driver;
248:            }
249:
250:            /**
251:             * @return the name of the JDBC driver.
252:             */
253:            public String getDriver() {
254:                return this .driver;
255:            }
256:
257:            /**
258:             * Sets the JNDI name.
259:             * @param jndiName the name to bind the datasource
260:             */
261:            public void setJndiName(final String jndiName) {
262:                this .jndiName = jndiName;
263:            }
264:
265:            /**
266:             * @return the name that is bound in the datasource
267:             */
268:            public String getJndiName() {
269:                return this .jndiName;
270:            }
271:
272:            /**
273:             * Sets the password to use.
274:             * @param password the password for the url connection.
275:             */
276:            public void setPassword(final String password) {
277:                this .password = password;
278:            }
279:
280:            /**
281:             * The maximum size of the JDBC pool.
282:             * @param poolMax the value of the pool's max.
283:             */
284:            public void setPoolMax(final int poolMax) {
285:                this .poolMax = poolMax;
286:            }
287:
288:            /**
289:             * @return the maximum size of the JDBC pool.
290:             */
291:            public int getPoolMax() {
292:                return this .poolMax;
293:            }
294:
295:            /**
296:             * The minimum size of the JDBC pool.
297:             * @param poolMin the value of the pool's min.
298:             */
299:            public void setPoolMin(final int poolMin) {
300:                this .poolMin = poolMin;
301:            }
302:
303:            /**
304:             * @return the minimum size of the JDBC pool.
305:             */
306:            public int getPoolMin() {
307:                return this .poolMax;
308:            }
309:
310:            /**
311:             * Set the max cache of prepared statement.
312:             * @param pstmtMax the max value for prepare statement.
313:             */
314:            public void setPstmtMax(final int pstmtMax) {
315:                this .pstmtMax = pstmtMax;
316:            }
317:
318:            /**
319:             * @return the minimum size of the JDBC pool.
320:             */
321:            public int getPstmtMax() {
322:                return this .poolMax;
323:            }
324:
325:            /**
326:             * Sets the connection's URL.
327:             * @param url the URL used for the connection.
328:             */
329:            public void setUrl(final String url) {
330:                this .url = url;
331:            }
332:
333:            /**
334:             * @return the URL used for the connection.
335:             */
336:            public String getUrl() {
337:                return this .url;
338:            }
339:
340:            /**
341:             * Sets the username that is used to get a connection.
342:             * @param username the name of the user.
343:             */
344:            public void setUsername(final String username) {
345:                this .username = username;
346:            }
347:
348:            /**
349:             * @return the username that is used to get a connection.
350:             */
351:            public String getUsername() {
352:                return this .username;
353:            }
354:
355:            /**
356:             * Is that the pool will use transaction or not.
357:             * @param useTM the true/false value.
358:             */
359:            public void setUseTM(final boolean useTM) {
360:                this .useTM = useTM;
361:            }
362:
363:            /**
364:             * @return true if this pool will use transaction (else false).
365:             */
366:            public boolean isUseTM() {
367:                return this .useTM;
368:            }
369:
370:            /**
371:             * @return connection checking level
372:             */
373:            public int getCheckLevel() {
374:                return this .checkLevel;
375:            }
376:
377:            /**
378:             * Sets the JDBC check level.
379:             * @param checkLevel jdbc connection checking level.
380:             */
381:            public void setCheckLevel(final int checkLevel) {
382:                this.checkLevel = checkLevel;
383:            }
384:
385:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.