Source Code Cross Referenced for TestOpenClose.java in  » Database-DBMS » h2database » org » h2 » test » db » 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 DBMS » h2database » org.h2.test.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (license2)
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.test.db;
007:
008:        import java.sql.Connection;
009:        import java.sql.DriverManager;
010:        import java.sql.PreparedStatement;
011:        import java.sql.ResultSet;
012:        import java.sql.SQLException;
013:        import java.sql.Statement;
014:
015:        import org.h2.api.DatabaseEventListener;
016:        import org.h2.test.TestBase;
017:        import org.h2.tools.Restore;
018:
019:        /**
020:         * Tests opening and closing a database.
021:         */
022:        public class TestOpenClose extends TestBase implements 
023:                DatabaseEventListener {
024:
025:            int nextId = 10;
026:
027:            public static void main(String[] a) throws Exception {
028:                new TestOpenClose().test();
029:            }
030:
031:            public void test() throws Exception {
032:                testBackup(false);
033:                testBackup(true);
034:                testCase();
035:                testReconnectFast();
036:            }
037:
038:            private void testBackup(boolean encrypt) throws Exception {
039:                deleteDb(baseDir, "openClose");
040:                String url;
041:                if (encrypt) {
042:                    url = "jdbc:h2:" + baseDir + "/openClose;CIPHER=XTEA";
043:                } else {
044:                    url = "jdbc:h2:" + baseDir + "/openClose";
045:                }
046:                org.h2.Driver.load();
047:                Connection conn = DriverManager.getConnection(url, "sa",
048:                        "abc def");
049:                Statement stat = conn.createStatement();
050:                stat.execute("CREATE TABLE TEST(C CLOB)");
051:                stat.execute("INSERT INTO TEST VALUES(SPACE(10000))");
052:                stat.execute("BACKUP TO '" + baseDir + "/test.zip'");
053:                conn.close();
054:                deleteDb(baseDir, "openClose");
055:                Restore.execute(baseDir + "/test.zip", baseDir, null, true);
056:                conn = DriverManager.getConnection(url, "sa", "abc def");
057:                stat = conn.createStatement();
058:                ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
059:                rs.next();
060:                check(rs.getString(1).length(), 10000);
061:                checkFalse(rs.next());
062:                conn.close();
063:            }
064:
065:            private void testReconnectFast() throws Exception {
066:                deleteDb(baseDir, "openClose");
067:                String url = "jdbc:h2:" + baseDir
068:                        + "/openClose;DATABASE_EVENT_LISTENER='"
069:                        + TestOpenClose.class.getName() + "'";
070:                Connection conn = DriverManager.getConnection(url, "sa", "sa");
071:                Statement stat = conn.createStatement();
072:                try {
073:                    stat
074:                            .execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
075:                    stat.execute("SET MAX_MEMORY_UNDO 100000");
076:                    stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)");
077:                    stat
078:                            .execute("INSERT INTO TEST SELECT X, X || ' Data' FROM SYSTEM_RANGE(1, 1000)");
079:                } catch (SQLException e) {
080:                    // ok
081:                }
082:                stat.close();
083:                conn.close();
084:                conn = DriverManager.getConnection(url, "sa", "sa");
085:                stat = conn.createStatement();
086:                ResultSet rs = stat.executeQuery("SELECT * FROM DUAL");
087:                if (rs.next()) {
088:                    rs.getString(1);
089:                }
090:                rs.close();
091:                stat.close();
092:                conn.close();
093:                conn = DriverManager.getConnection(url, "sa", "sa");
094:                stat = conn.createStatement();
095:                // stat.execute("SET DB_CLOSE_DELAY 0");
096:                stat.executeUpdate("SHUTDOWN");
097:                stat.close();
098:                conn.close();
099:            }
100:
101:            void testCase() throws Exception {
102:                Class.forName("org.h2.Driver");
103:                deleteDb(baseDir, "openClose");
104:                final String url = "jdbc:h2:" + baseDir
105:                        + "/openClose;FILE_LOCK=NO";
106:                Connection conn = DriverManager.getConnection(url, "sa", "");
107:                conn.createStatement().execute("drop table employee if exists");
108:                conn
109:                        .createStatement()
110:                        .execute(
111:                                "create table employee(id int primary key, name varchar, salary int)");
112:                conn.close();
113:                int len = this .getSize(200, 4000);
114:                Thread[] threads = new Thread[len];
115:                for (int i = 0; i < len; i++) {
116:                    threads[i] = new Thread() {
117:                        public void run() {
118:                            try {
119:                                Connection conn = DriverManager.getConnection(
120:                                        url, "sa", "");
121:                                PreparedStatement prep = conn
122:                                        .prepareStatement("insert into employee values(?, ?, 0)");
123:                                int id = getNextId();
124:                                prep.setInt(1, id);
125:                                prep.setString(2, "employee " + id);
126:                                prep.execute();
127:                                conn.close();
128:                            } catch (Throwable e) {
129:                                TestBase.logError("insert", e);
130:                            }
131:                        }
132:                    };
133:                    threads[i].start();
134:                }
135:                // for(int i=0; i<len; i++) {
136:                // threads[i].start();
137:                // }
138:                for (int i = 0; i < len; i++) {
139:                    threads[i].join();
140:                }
141:                conn = DriverManager.getConnection(url, "sa", "");
142:                ResultSet rs = conn.createStatement().executeQuery(
143:                        "select count(*) from employee");
144:                rs.next();
145:                check(rs.getInt(1), len);
146:                conn.close();
147:            }
148:
149:            synchronized int getNextId() {
150:                return nextId++;
151:            }
152:
153:            public void diskSpaceIsLow(long stillAvailable) throws SQLException {
154:                throw new SQLException("unexpected");
155:            }
156:
157:            public void exceptionThrown(SQLException e, String sql) {
158:                throw new Error("unexpected: " + e + " sql: " + sql);
159:            }
160:
161:            public void setProgress(int state, String name, int current, int max) {
162:                String stateName;
163:                switch (state) {
164:                case STATE_SCAN_FILE:
165:                    stateName = "Scan " + name + " " + current + "/" + max;
166:                    if (current > 0) {
167:                        throw new Error("unexpected: " + stateName);
168:                    }
169:                    break;
170:                case STATE_CREATE_INDEX:
171:                    stateName = "Create Index " + name + " " + current + "/"
172:                            + max;
173:                    if (!"SYS".equals(name)) {
174:                        throw new Error("unexpected: " + stateName);
175:                    }
176:                    break;
177:                case STATE_RECOVER:
178:                    stateName = "Recover " + current + "/" + max;
179:                    break;
180:                default:
181:                    stateName = "?";
182:                }
183:                // System.out.println(": " + stateName);
184:            }
185:
186:            public void closingDatabase() {
187:            }
188:
189:            public void init(String url) {
190:            }
191:
192:            public void opened() {
193:            }
194:
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.