Source Code Cross Referenced for ConnectionPool.java in  » Chat » freecs » freecs » auth » sqlConnectionPool » 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 » Chat » freecs » freecs.auth.sqlConnectionPool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) 2003  Manfred Andres
003:         * 
004:         * This program is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU General Public License
006:         * as published by the Free Software Foundation; either version 2
007:         * of the License, or (at your option) any later version.
008:         * 
009:         * This program 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
012:         * GNU General Public License for more details.
013:         * 
014:         * You should have received a copy of the GNU General Public License
015:         * along with this program; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017:         */package freecs.auth.sqlConnectionPool;
018:
019:        import java.sql.*;
020:        import freecs.Server;
021:        import freecs.auth.SQLAuthenticator;
022:        import freecs.core.CanceledRequestException;
023:        import freecs.core.ConnectionBuffer;
024:
025:        public class ConnectionPool {
026:
027:            final SQLAuthenticator authenticator;
028:            final DbProperties dbProps;
029:            final PoolElement pool[];
030:            volatile int p = 0, idCnt = 0;
031:
032:            /**
033:             * Construct a new ConnectionPool for the given authenticator 
034:             * and the given dbProperties
035:             * @param authenticator the authenticator this ConnectionPool will used by.
036:             * @param dbProps the dbProperties describing the datastructure
037:             */
038:            public ConnectionPool(SQLAuthenticator authenticator,
039:                    DbProperties dbProps) {
040:                this .authenticator = authenticator;
041:                this .dbProps = dbProps;
042:                pool = new PoolElement[dbProps.poolsize];
043:                if (Server.TRACE_CREATE_AND_FINALIZE) {
044:                    Server.log(this ,
045:                            "++++++++++++++++++++++++++++++++++++++++CREATE",
046:                            Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
047:                }
048:            }
049:
050:            /**
051:             * close every connection to the jdbc-source and remove the PooleElements 
052:             */
053:            public void shutdown() {
054:                synchronized (this .pool) {
055:                    for (int i = 0; i < pool.length; i++) {
056:                        if (pool[i] == null) {
057:                            continue;
058:                        }
059:                        pool[i].cleanup();
060:                        pool[i] = null;
061:                    }
062:                }
063:            }
064:
065:            /**
066:             * creates an connectionpool-element
067:             * @return the PoolElement connected to the jdbc-datasource
068:             * @throws Exception
069:             */
070:            private PoolElement createPoolElement() throws Exception {
071:                idCnt++;
072:                if (idCnt == Integer.MAX_VALUE) {
073:                    idCnt = 0;
074:                }
075:                Connection con = DriverManager.getConnection(dbProps.url,
076:                        dbProps.conProps);
077:                return new PoolElement(this , con, dbProps, idCnt);
078:            }
079:
080:            public PoolElement getPoolElement(int retrys, ConnectionBuffer cb)
081:                    throws Exception {
082:                // try [retrys] times to retrieve PoolElement
083:                for (int i = 0; i < retrys; i++) {
084:                    if ((cb != null && !cb.isValid())
085:                            || Thread.currentThread().isInterrupted())
086:                        throw new CanceledRequestException(
087:                                "ConnectionBuffer has been invalidated");
088:
089:                    PoolElement el = null;
090:                    try {
091:                        el = this .getPoolElement(cb);
092:                        if (el != null) // if el isn't null, return it (it was checked for validity)
093:                            return el;
094:                    } catch (CanceledRequestException cre) {
095:                        throw cre;
096:                    } catch (Exception e) {
097:                        Server.debug(Thread.currentThread(), this .toString()
098:                                + "getPoolElement: ", e, Server.MSG_AUTH,
099:                                Server.LVL_MAJOR);
100:                        el = null;
101:                    }
102:                }
103:                throw new Exception("Unable to get available PoolElement");
104:            }
105:
106:            /**
107:             * Gets the next available and valid poolelement or tryes to create a new one
108:             * @return the valid and available PoolElement
109:             * @throws Exception If creation of PoolElement failes
110:             */
111:            private PoolElement getPoolElement(ConnectionBuffer cb)
112:                    throws Exception {
113:                for (int i = 0; i < dbProps.poolsize; i++) {
114:                    if ((cb != null && !cb.isValid())
115:                            || Thread.currentThread().isInterrupted())
116:                        throw new CanceledRequestException(
117:                                "ConnectionBuffer has been invalidated");
118:                    p++;
119:                    if (p > dbProps.poolsize - 1)
120:                        p = 0;
121:                    synchronized (this .pool) {
122:                        if (pool[p] == null) {
123:                            pool[p] = createPoolElement();
124:                        }
125:                        switch (pool[p].grab()) {
126:                        case PoolElement.INVALID:
127:                            pool[p].cleanup();
128:                            pool[p] = createPoolElement();
129:                        case PoolElement.IDLE:
130:                            return pool[p];
131:                        case PoolElement.ACTIVE:
132:                            continue;
133:                        }
134:                    }
135:                }
136:                return null;
137:            }
138:
139:            public int size() {
140:                return pool.length;
141:            }
142:
143:            public void finalize() {
144:                if (Server.TRACE_CREATE_AND_FINALIZE)
145:                    Server
146:                            .log(
147:                                    this ,
148:                                    "----------------------------------------FINALIZED",
149:                                    Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
150:            }
151:
152:            private ConnectionPool() throws Exception {
153:                throw new Exception("not instatiable without arguments");
154:            }
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.