Source Code Cross Referenced for DBConnectionManager.java in  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » loader » 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 » Database JDBC Connection Pool » octopus » org.webdocwf.util.loader 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          Loader - tool for transfering data from one JDBC source to another and
003:          doing transformations during copy.
004:            Copyright (C) 2002-2003  Together
005:            This library is free software; you can redistribute it and/or
006:            modify it under the terms of the GNU Lesser General Public
007:            License as published by the Free Software Foundation; either
008:            version 2.1 of the License, or (at your option) any later version.
009:            This library is distributed in the hope that it will be useful,
010:            but WITHOUT ANY WARRANTY; without even the implied warranty of
011:            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:            Lesser General Public License for more details.
013:            You should have received a copy of the GNU Lesser General Public
014:            License along with this library; if not, write to the Free Software
015:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
016:         Loader.java
017:         Date: 03.03.2003.
018:         @version 2.1 alpha
019:         @authors:
020:         Milosevic Sinisa sinisa@prozone.yu
021:         Radoslav Dutina  rale@prozone.co.yu
022:         */
023:
024:        package org.webdocwf.util.loader;
025:
026:        import java.io.*;
027:        import java.util.*;
028:        import java.sql.*;
029:
030:        /**
031:         * DBConnectionManager class creates all connection to database.
032:         * @author Radoslav Dutina
033:         * @version 1.0
034:         */
035:        public class DBConnectionManager {
036:            private Vector drivers = new Vector();
037:            private Hashtable pools = new Hashtable();
038:            private static DBConnectionManager instance; // The single instance
039:            private Hashtable openConnections = new Hashtable();
040:            private Vector allConnectons = new Vector();
041:
042:            public String loaderJobPath = "";
043:            public String connectinPrefix = "";
044:            public boolean fileSystemDatabase = false;
045:
046:            /**
047:             * This method set all connection into vector
048:             * @param allConnections represents all connection property
049:             */
050:            public DBConnectionManager(Vector allConnections) {
051:                this .allConnectons = allConnections;
052:                init();
053:            }
054:
055:            /**
056:             * This method set path to LoaderJob.olj file
057:             * @param loaderJob represents string path to LoaderJob.olj file
058:             */
059:            public void setLoaderJobPath(String loaderJob) {
060:                File file = new File(loaderJob);
061:                this .loaderJobPath = file.getAbsoluteFile().getParent()
062:                        + System.getProperty("file.separator");
063:            }
064:
065:            /**
066:             * This method set connection prefix
067:             * @param prefix is value of connection prefix
068:             */
069:            public void setConnectionPrefix(String prefix) {
070:                this .connectinPrefix = prefix;
071:            }
072:
073:            /**
074:             * This method set the value of fileSystemDatabase parameter
075:             * @param doParse is value of parameter
076:             */
077:            public void setParsePermission(boolean doParse) {
078:                this .fileSystemDatabase = doParse;
079:            }
080:
081:            /**
082:             * This method initialized DBConnectionManager object
083:             */
084:            private void init() {
085:
086:                loadDrivers(allConnectons);
087:                createPools(allConnectons);
088:            }
089:
090:            private void loadDrivers(Vector allConnectons) {
091:                if (allConnectons.size() != 0) {
092:                    for (int i = 0; i < allConnectons.size(); i = i + 4) {
093:                        if (i > 3) {
094:                            if (allConnectons
095:                                    .get(i)
096:                                    .toString()
097:                                    .equalsIgnoreCase(
098:                                            allConnectons.get(i - 4).toString())) {
099:                                //do nothing
100:                            } else {
101:                                try {
102:                                    Driver driver = (Driver) Class.forName(
103:                                            allConnectons.get(i).toString())
104:                                            .newInstance();
105:                                    DriverManager.registerDriver(driver);
106:                                    drivers.add(driver);
107:                                } catch (Exception e) {
108:                                    e.getMessage();
109:                                }
110:                            }
111:                        } else {
112:                            try {
113:                                Driver driver = (Driver) Class.forName(
114:                                        allConnectons.get(i).toString())
115:                                        .newInstance();
116:                                DriverManager.registerDriver(driver);
117:                                drivers.add(driver);
118:                            } catch (Exception e) {
119:                                e.getMessage();
120:                            }
121:                        }
122:                    }
123:                }
124:            }
125:
126:            private void createPools(Vector allConnectons) {
127:                for (int i = 0; i < allConnectons.size(); i = i + 4) {
128:                    String url = allConnectons.get(i + 1).toString();
129:                    if (url.indexOf("jdbc:microsoft:sqlserver") != -1) {
130:                        if (url.indexOf("SelectMethod") == -1) {
131:                            url = url + ";SelectMethod=cursor";
132:                        }
133:                    }
134:                    String poolName = url;
135:                    String user = allConnectons.get(i + 2).toString();
136:                    String password = allConnectons.get(i + 3).toString();
137:                    boolean check = false;
138:                    if (pools.size() > 0) {
139:                        check = pools.containsKey(poolName);
140:                    }
141:                    if (!check) {
142:                        DBConnectionPool pool = new DBConnectionPool(poolName,
143:                                url, user, password);
144:                        pools.put(poolName, pool);
145:                    }
146:                }
147:            }
148:
149:            /**
150:             * This method get connection from connection pool
151:             * @param name is the name of the pool
152:             * @return connection
153:             */
154:            public Connection getConnection(String name) {
155:                DBConnectionPool pool = (DBConnectionPool) pools.get(name);
156:                if (pool != null) {
157:                    return pool.getConnection();
158:                }
159:                return null;
160:            }
161:
162:            /**
163:             * This method release (close) all connection and deregister all drivers
164:             * @param exception defines if application calls release from exception method or not
165:             */
166:            public void release(String exception) {
167:                Enumeration allPools = pools.elements();
168:                while (allPools.hasMoreElements()) {
169:                    DBConnectionPool pool = (DBConnectionPool) allPools
170:                            .nextElement();
171:                    pool.release(exception);
172:                }
173:                Enumeration allDrivers = drivers.elements();
174:                while (allDrivers.hasMoreElements()) {
175:                    Driver driver = (Driver) allDrivers.nextElement();
176:                    try {
177:                        DriverManager.deregisterDriver(driver);
178:                    } catch (Exception e) {
179:                        e.getMessage();
180:                    }
181:                }
182:            }
183:
184:            private class DBConnectionPool {
185:                private String name = null;
186:                private String url = null;
187:                private String user = null;
188:                private String password = null;
189:
190:                /**
191:                 * Construct the object of DBConnectionPool class with associated parameters
192:                 * @param name is the name of the connection
193:                 * @param url is url to database which we wont to connect
194:                 * @param user is the user name
195:                 * @param password is user password
196:                 */
197:                public DBConnectionPool(String name, String url, String user,
198:                        String password) {
199:                    this .name = name;
200:                    this .url = url;
201:                    this .user = user;
202:                    this .password = password;
203:
204:                }
205:
206:                /**
207:                 * This method return connection if the connection exists, or create new connection
208:                 * if don't
209:                 * @return connection
210:                 */
211:                public Connection getConnection() {
212:                    Connection conn = null;
213:                    if (openConnections.size() > 0) {
214:                        conn = (Connection) openConnections.get(name);
215:                        if (conn != null) {
216:                            return conn;
217:                        }
218:                        conn = newConnections();
219:                    } else {
220:                        conn = newConnections();
221:                    }
222:                    return conn;
223:                }
224:
225:                private Connection newConnections() {
226:                    Connection conn = null;
227:                    url = Utils.getAbsolutePathFromDatabaseURL(connectinPrefix,
228:                            loaderJobPath, url, fileSystemDatabase);
229:                    try {
230:                        if (user == null) {
231:                            conn = DriverManager.getConnection(url);
232:                            openConnections.put(name, conn);
233:                        } else {
234:                            conn = DriverManager.getConnection(url, user,
235:                                    password);
236:                            openConnections.put(name, conn);
237:                        }
238:
239:                    } catch (Exception e) {
240:                        e.printStackTrace();
241:                    }
242:                    return conn;
243:                }
244:
245:                /**
246:                 * This method close and commit connection
247:                 * @param exception defines if application calls release from exception method or not
248:                 */
249:                public void release(String exception) {
250:                    Connection conn = (Connection) openConnections.get(name);
251:                    try {
252:                        if (!conn.isClosed()) {
253:                            if (exception.equalsIgnoreCase("false"))
254:                                conn.commit();
255:                            conn.close();
256:                        }
257:                        openConnections.remove(name);
258:                    } catch (Exception e) {
259:                        e.getMessage();
260:                    }
261:                }
262:
263:            }
264:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.