1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.commsen.stopwatch.storages;
18
19 import java.sql.SQLException;
20 import java.sql.Statement;
21
22 import org.apache.log4j.Logger;
23
24 import com.commsen.stopwatch.StopwatchStorageException;
25
26 /***
27 * Default stopwatch storage. It uses in-memory HSQL database.
28 *
29 *
30 * @author Milen Dyankov
31 *
32 */
33 public class DefaultHSQLInMemoryStorage extends AbstractDatabaseStorage {
34
35 /***
36 * Logger for this class
37 */
38 private static final Logger log = Logger.getLogger(DefaultHSQLInMemoryStorage.class);
39
40
41 protected String getDriver() {
42 return "org.hsqldb.jdbcDriver";
43 }
44
45
46 protected String getConnectionString() {
47 return "jdbc:hsqldb:mem:stopwatch";
48 }
49
50
51 protected String getUser() {
52 return "sa";
53 }
54
55
56 protected String getPassword() {
57 return "";
58 }
59
60
61 protected String getLastIdentityQuery() {
62 return "CALL IDENTITY()";
63 }
64
65
66 protected String getTableName() {
67 return "stopwatch";
68 }
69
70
71 protected String getReturnColumns() {
72 return " count(1), " + " min (DATEDIFF('ms', _start, _end)) as minTime," + " max (DATEDIFF('ms', _start, _end)) as maxTime," + " avg (DATEDIFF('ms', _start, _end)) as avgTime,"
73 + " sum (DATEDIFF('ms', _start, _end)) as totalTime ";
74 }
75
76
77 protected String getTruncTableQuery() {
78 return null;
79 }
80
81 private boolean debugEnabled;
82
83
84 /***
85 * @see com.commsen.stopwatch.StopwatchStorage#freeze()
86 */
87 public void freeze() throws StopwatchStorageException {
88 Statement statement = null;
89 try {
90 statement = updateConnection.createStatement();
91 statement.execute("delete from stopwatch where _end is NULL");
92 } catch (SQLException e) {
93 getLogger().error(e, e);
94 } finally {
95 if (statement != null) {
96 try {
97 statement.close();
98 } catch (SQLException e) {
99 getLogger().error(e, e);
100 }
101 }
102 }
103 }
104
105
106 /***
107 *
108 * @throws StopwatchStorageException
109 * @see com.commsen.stopwatch.StopwatchStorage#close()
110 */
111 public void close() throws StopwatchStorageException {
112 Statement statement = null;
113 try {
114 statement = updateConnection.createStatement();
115 statement.execute("SHUTDOWN");
116 } catch (SQLException e) {
117 throw new StopwatchStorageException("database error", e);
118 } finally {
119 if (statement != null) {
120 try {
121 statement.close();
122 } catch (SQLException e) {
123 throw new StopwatchStorageException("database error", e);
124 }
125 }
126 }
127 super.close();
128 }
129
130
131 protected Logger getLogger() {
132 return log;
133 }
134
135
136 /***
137 * @return Returns the debugEnabled.
138 */
139 public boolean isDebugEnabled() {
140 return debugEnabled;
141 }
142
143
144 /***
145 * @param debugEnabled The debugEnabled to set.
146 */
147 public void setDebugEnabled(boolean debugEnabled) {
148 this.debugEnabled = debugEnabled;
149 }
150
151 }