Source Code Cross Referenced for PSLoadPoolBackedDataSource.java in  » Database-JDBC-Connection-Pool » c3p0 » com » mchange » v2 » c3p0 » test » 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 » c3p0 » com.mchange.v2.c3p0.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Distributed as part of c3p0 v.0.9.1.2
003:         *
004:         * Copyright (C) 2005 Machinery For Change, Inc.
005:         *
006:         * Author: Steve Waldman <swaldman@mchange.com>
007:         *
008:         * This library is free software; you can redistribute it and/or modify
009:         * it under the terms of the GNU Lesser General Public License version 2.1, as 
010:         * published by the Free Software Foundation.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:         * GNU Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public License
018:         * along with this software; see the file LICENSE.  If not, write to the
019:         * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020:         * Boston, MA 02111-1307, USA.
021:         */
022:
023:        package com.mchange.v2.c3p0.test;
024:
025:        import java.util.*;
026:        import java.sql.*;
027:        import javax.sql.*;
028:        import com.mchange.v2.c3p0.*;
029:        import com.mchange.v1.db.sql.*;
030:
031:        public final class PSLoadPoolBackedDataSource {
032:            final static String INSERT_STMT = "INSERT INTO testpbds VALUES ( ? , ? )";
033:            final static String SELECT_STMT = "SELECT count(*) FROM testpbds";
034:            final static String DELETE_STMT = "DELETE FROM testpbds";
035:
036:            static Random random = new Random();
037:            static DataSource ds;
038:
039:            public static void main(String[] argv) {
040:                if (argv.length > 0) {
041:                    System.err
042:                            .println(PSLoadPoolBackedDataSource.class.getName()
043:                                    + " now requires no args. Please set everything in standard c3p0 config files.");
044:                    return;
045:                }
046:
047:                String jdbc_url = null;
048:                String username = null;
049:                String password = null;
050:
051:                /*
052:                if (argv.length == 3)
053:                {
054:                jdbc_url = argv[0];
055:                username = argv[1];
056:                password = argv[2];
057:                }
058:                else if (argv.length == 1)
059:                {
060:                jdbc_url = argv[0];
061:                username = null;
062:                password = null;
063:                }
064:                else
065:                usage();
066:                
067:                if (! jdbc_url.startsWith("jdbc:") )
068:                usage();
069:                 */
070:
071:                try {
072:                    //DataSource ds_unpooled = DataSources.unpooledDataSource(jdbc_url, username, password);
073:                    //DataSource ds_unpooled = new FreezableDriverManagerDataSource();
074:
075:                    DataSource ds_unpooled = DataSources.unpooledDataSource();
076:                    ds = DataSources.pooledDataSource(ds_unpooled);
077:
078:                    //new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
079:
080:                    Connection con = null;
081:                    Statement stmt = null;
082:
083:                    try {
084:                        con = ds_unpooled.getConnection();
085:                        stmt = con.createStatement();
086:                        stmt
087:                                .executeUpdate("CREATE TABLE testpbds ( a varchar(16), b varchar(16) )");
088:                    } catch (SQLException e) {
089:                        e.printStackTrace();
090:                        System.err
091:                                .println("relation testpbds already exists, or something "
092:                                        + "bad happened.");
093:                    } finally {
094:                        StatementUtils.attemptClose(stmt);
095:                        ConnectionUtils.attemptClose(con);
096:                    }
097:
098:                    //for (int i = 0; i < 5; ++i)
099:                    for (int i = 0; i < 50; ++i) {
100:                        Thread t = new ChurnThread();
101:                        t.start();
102:                        System.out.println("THREAD MADE [" + i + "]");
103:                        Thread.sleep(1000);
104:                    }
105:
106:                } catch (Exception e) {
107:                    e.printStackTrace();
108:                }
109:            }
110:
111:            static class ChurnThread extends Thread {
112:                public void run() {
113:                    try {
114:                        while (true) {
115:                            Connection con = null;
116:                            try {
117:                                con = ds.getConnection();
118:                                int select = random.nextInt(3);
119:                                switch (select) {
120:                                case 0:
121:                                    executeSelect(con);
122:                                    break;
123:                                case 1:
124:                                    executeInsert(con);
125:                                    break;
126:                                case 2:
127:                                    executeDelete(con);
128:                                    break;
129:                                }
130:                            } catch (Exception e) {
131:                                e.printStackTrace();
132:                            } finally {
133:                                ConnectionUtils.attemptClose(con);
134:                            }
135:
136:                            //Thread.sleep( random.nextInt( 1000 ) );
137:                        }
138:                    } catch (Exception e) {
139:                        e.printStackTrace();
140:                    }
141:                }
142:            }
143:
144:            static void executeInsert(Connection con) throws SQLException {
145:                PreparedStatement pstmt = null;
146:                try {
147:                    pstmt = con.prepareStatement(INSERT_STMT);
148:                    pstmt.setInt(1, random.nextInt());
149:                    pstmt.setInt(2, random.nextInt());
150:                    pstmt.executeUpdate();
151:                    System.out.println("INSERTION");
152:                } finally {
153:                    // make sure forgetting this doesn't starve
154:                    // statement cache, as long as the connection
155:                    // closes...
156:
157:                    StatementUtils.attemptClose(pstmt);
158:                }
159:            }
160:
161:            static void executeSelect(Connection con) throws SQLException {
162:                long l = System.currentTimeMillis();
163:                PreparedStatement pstmt = null;
164:                ResultSet rs = null;
165:                try {
166:                    pstmt = con.prepareStatement(SELECT_STMT);
167:                    rs = pstmt.executeQuery();
168:                    rs.next(); //we assume one row, one col
169:                    System.out.println("SELECT [count=" + rs.getInt(1)
170:                            + ", time=" + (System.currentTimeMillis() - l)
171:                            + " msecs]");
172:                } finally {
173:                    ResultSetUtils.attemptClose(rs);
174:                    StatementUtils.attemptClose(pstmt);
175:                }
176:            }
177:
178:            static void executeDelete(Connection con) throws SQLException {
179:                PreparedStatement pstmt = null;
180:                ResultSet rs = null;
181:                try {
182:                    pstmt = con.prepareStatement(DELETE_STMT);
183:                    int deleted = pstmt.executeUpdate();
184:                    System.out.println("DELETE [" + deleted + " rows]");
185:                } finally {
186:                    ResultSetUtils.attemptClose(rs);
187:                    StatementUtils.attemptClose(pstmt);
188:                }
189:            }
190:
191:            /*
192:            private static void usage()
193:            {
194:            System.err.println("java " +
195:            		   "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
196:            		   PSLoadPoolBackedDataSource.class.getName() +
197:            		   " <jdbc_url> [<username> <password>]" );
198:            System.exit(-1);
199:            }
200:             */
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.