Source Code Cross Referenced for TestAbandonedObjectPool.java in  » Database-JDBC-Connection-Pool » Connection-Pool-DBCP » org » apache » commons » dbcp » 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 » Connection Pool DBCP » org.apache.commons.dbcp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.dbcp;
019:
020:        import java.util.Vector;
021:
022:        import junit.framework.Test;
023:        import junit.framework.TestCase;
024:        import junit.framework.TestSuite;
025:
026:        import org.apache.commons.pool.PoolableObjectFactory;
027:
028:        /**
029:         * TestCase for AbandonedObjectPool
030:         * 
031:         * @author Wayne Woodfield
032:         * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
033:         */
034:        public class TestAbandonedObjectPool extends TestCase {
035:            private AbandonedObjectPool pool = null;
036:            private AbandonedConfig config = null;
037:
038:            public TestAbandonedObjectPool(String testName) {
039:                super (testName);
040:            }
041:
042:            public static Test suite() {
043:                return new TestSuite(TestAbandonedObjectPool.class);
044:            }
045:
046:            public void setUp() throws Exception {
047:                super .setUp();
048:                config = new AbandonedConfig();
049:
050:                // -- Uncomment the following line to enable logging -- 
051:                // config.setLogAbandoned(true);
052:
053:                config.setRemoveAbandoned(true);
054:                config.setRemoveAbandonedTimeout(1);
055:                pool = new AbandonedObjectPool(new SimpleFactory(), config);
056:            }
057:
058:            public void tearDown() throws Exception {
059:                super .tearDown();
060:                pool.close();
061:                pool = null;
062:            }
063:
064:            /**
065:             * Tests fix for Bug 28579, a bug in AbandonedObjectPool that causes numActive to go negative
066:             * in GenericObjectPool
067:             */
068:            public void testConcurrentInvalidation() throws Exception {
069:                final int POOL_SIZE = 30;
070:                pool.setMaxActive(POOL_SIZE);
071:                pool.setMaxIdle(POOL_SIZE);
072:                pool
073:                        .setWhenExhaustedAction(AbandonedObjectPool.WHEN_EXHAUSTED_FAIL);
074:
075:                // Exhaust the connection pool
076:                Vector vec = new Vector();
077:                for (int i = 0; i < POOL_SIZE; i++) {
078:                    vec.add(pool.borrowObject());
079:                }
080:
081:                // Abandon all borrowed objects
082:                for (int i = 0; i < vec.size(); i++) {
083:                    ((PooledTestObject) vec.elementAt(i)).setAbandoned(true);
084:                }
085:
086:                // Try launching a bunch of borrows concurrently.  Abandoned sweep will be triggered for each.
087:                final int CONCURRENT_BORROWS = 5;
088:                Thread[] threads = new Thread[CONCURRENT_BORROWS];
089:                for (int i = 0; i < CONCURRENT_BORROWS; i++) {
090:                    threads[i] = new ConcurrentBorrower(vec);
091:                    threads[i].start();
092:                }
093:
094:                // Wait for all the threads to finish
095:                for (int i = 0; i < CONCURRENT_BORROWS; i++) {
096:                    threads[i].join();
097:                }
098:
099:                // Return all objects that have not been destroyed
100:                for (int i = 0; i < vec.size(); i++) {
101:                    PooledTestObject pto = (PooledTestObject) vec.elementAt(i);
102:                    if (pto.isActive()) {
103:                        pool.returnObject(pto);
104:                    }
105:                }
106:
107:                // Now, the number of open connections should be 0
108:                assertTrue("numActive should have been 0, was "
109:                        + pool.getNumActive(), pool.getNumActive() == 0);
110:            }
111:
112:            class ConcurrentBorrower extends Thread {
113:                private Vector _borrowed;
114:
115:                public ConcurrentBorrower(Vector borrowed) {
116:                    _borrowed = borrowed;
117:                }
118:
119:                public void run() {
120:                    try {
121:                        _borrowed.add(pool.borrowObject());
122:                    } catch (Exception e) {
123:                        // expected in most cases
124:                    }
125:                }
126:            }
127:
128:            class SimpleFactory implements  PoolableObjectFactory {
129:
130:                public Object makeObject() {
131:                    return new PooledTestObject(config);
132:                }
133:
134:                public boolean validateObject(Object obj) {
135:                    return true;
136:                }
137:
138:                public void activateObject(Object obj) {
139:                    ((PooledTestObject) obj).setActive(true);
140:                }
141:
142:                public void passivateObject(Object obj) {
143:                    ((PooledTestObject) obj).setActive(false);
144:                }
145:
146:                public void destroyObject(Object obj) {
147:                    ((PooledTestObject) obj).setActive(false);
148:                    // while destroying connections, yield control to other threads
149:                    // helps simulate threading errors
150:                    Thread.yield();
151:                }
152:            }
153:        }
154:
155:        class PooledTestObject extends AbandonedTrace {
156:            private boolean active = false;
157:            private int _hash = 0;
158:            private boolean _abandoned = false;
159:
160:            private static int hash = 1;
161:
162:            public PooledTestObject(AbandonedConfig config) {
163:                super (config);
164:                _hash = hash++;
165:            }
166:
167:            public synchronized void setActive(boolean b) {
168:                active = b;
169:            }
170:
171:            public synchronized boolean isActive() {
172:                return active;
173:            }
174:
175:            public int hashCode() {
176:                return _hash;
177:            }
178:
179:            public void setAbandoned(boolean b) {
180:                _abandoned = b;
181:            }
182:
183:            protected long getLastUsed() {
184:                if (_abandoned) {
185:                    // Abandoned object sweep will occur no matter what the value of removeAbandonedTimeout,
186:                    // because this indicates that this object was last used decades ago
187:                    return 1;
188:                } else {
189:                    // Abandoned object sweep won't clean up this object
190:                    return 0;
191:                }
192:            }
193:
194:            public boolean equals(Object obj) {
195:                if (!(obj instanceof  PooledTestObject))
196:                    return false;
197:                return obj.hashCode() == hashCode();
198:            }
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.