1 package org.apache.commons.jcs.auxiliary.disk.jdbc.mysql;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26
27 import javax.sql.DataSource;
28
29 import org.apache.commons.jcs.auxiliary.disk.jdbc.TableState;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34
35
36
37
38
39
40
41
42 public class MySQLTableOptimizer
43 {
44
45 private static final Log log = LogFactory.getLog( MySQLTableOptimizer.class );
46
47
48 private DataSource dataSource = null;
49
50
51 private String tableName = null;
52
53
54 private TableState tableState;
55
56
57
58
59
60
61
62
63 public MySQLTableOptimizer( MySQLDiskCacheAttributes attributes, TableState tableState, DataSource dataSource )
64 {
65 setTableName( attributes.getTableName() );
66
67 this.tableState = tableState;
68 this.dataSource = dataSource;
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public boolean optimizeTable()
117 {
118 long start = System.currentTimeMillis();
119 boolean success = false;
120
121 if ( tableState.getState() == TableState.OPTIMIZATION_RUNNING )
122 {
123 log
124 .warn( "Skipping optimization. Optimize was called, but the table state indicates that an optimization is currently running." );
125 return false;
126 }
127
128 try
129 {
130 tableState.setState( TableState.OPTIMIZATION_RUNNING );
131 if ( log.isInfoEnabled() )
132 {
133 log.info( "Optimizing table [" + this.getTableName() + "]" );
134 }
135
136 Connection con;
137 try
138 {
139 con = dataSource.getConnection();
140 }
141 catch ( SQLException e )
142 {
143 log.error( "Problem getting connection.", e );
144 return false;
145 }
146
147 try
148 {
149
150 Statement sStatement = null;
151 try
152 {
153 sStatement = con.createStatement();
154
155 ResultSet rs = sStatement.executeQuery( "optimize table " + this.getTableName() );
156
157
158
159
160
161 if ( rs.next() )
162 {
163 String status = rs.getString( "Msg_type" );
164 String message = rs.getString( "Msg_text" );
165
166 if ( log.isInfoEnabled() )
167 {
168 log.info( "Message Type: " + status );
169 log.info( "Message: " + message );
170 }
171
172 if ( "error".equals( status ) )
173 {
174 log.warn( "Optimization was in error. Will attempt to repair the table. Message: "
175 + message );
176
177
178 success = repairTable( sStatement );
179 }
180 else
181 {
182 success = true;
183 }
184 }
185
186
187 String statusString = getTableStatus( sStatement );
188 if ( log.isInfoEnabled() )
189 {
190 log.info( "Table status after optimizing table [" + this.getTableName() + "]\n" + statusString );
191 }
192 }
193 catch ( SQLException e )
194 {
195 log.error( "Problem optimizing table [" + this.getTableName() + "]", e );
196 return false;
197 }
198 finally
199 {
200 if (sStatement != null)
201 {
202 try
203 {
204 sStatement.close();
205 }
206 catch ( SQLException e )
207 {
208 log.error( "Problem closing statement.", e );
209 }
210 }
211 }
212 }
213 finally
214 {
215 try
216 {
217 con.close();
218 }
219 catch ( SQLException e )
220 {
221 log.error( "Problem closing connection.", e );
222 }
223 }
224 }
225 finally
226 {
227 tableState.setState( TableState.FREE );
228
229 long end = System.currentTimeMillis();
230 if ( log.isInfoEnabled() )
231 {
232 log.info( "Optimization of table [" + this.getTableName() + "] took " + ( end - start ) + " ms." );
233 }
234 }
235
236 return success;
237 }
238
239
240
241
242
243
244
245
246 protected String getTableStatus( Statement sStatement )
247 throws SQLException
248 {
249 ResultSet statusResultSet = sStatement.executeQuery( "show table status" );
250 StringBuilder statusString = new StringBuilder();
251 int numColumns = statusResultSet.getMetaData().getColumnCount();
252 while ( statusResultSet.next() )
253 {
254 statusString.append( "\n" );
255 for ( int i = 1; i <= numColumns; i++ )
256 {
257 statusString.append( statusResultSet.getMetaData().getColumnLabel( i ) + " ["
258 + statusResultSet.getString( i ) + "] | " );
259 }
260 }
261 return statusString.toString();
262 }
263
264
265
266
267
268
269
270
271
272
273
274 protected boolean repairTable( Statement sStatement )
275 throws SQLException
276 {
277 boolean success = false;
278
279
280 ResultSet repairResult = sStatement.executeQuery( "repair table " + this.getTableName() );
281 StringBuilder repairString = new StringBuilder();
282 int numColumns = repairResult.getMetaData().getColumnCount();
283 while ( repairResult.next() )
284 {
285 for ( int i = 1; i <= numColumns; i++ )
286 {
287 repairString.append( repairResult.getMetaData().getColumnLabel( i ) + " [" + repairResult.getString( i )
288 + "] | " );
289 }
290
291 String message = repairResult.getString( "Msg_text" );
292 if ( "OK".equals( message ) )
293 {
294 success = true;
295 }
296 }
297 if ( log.isInfoEnabled() )
298 {
299 log.info( repairString );
300 }
301
302 if ( !success )
303 {
304 log.warn( "Failed to repair the table. " + repairString );
305 }
306 return success;
307 }
308
309
310
311
312 public void setTableName( String tableName )
313 {
314 this.tableName = tableName;
315 }
316
317
318
319
320 public String getTableName()
321 {
322 return tableName;
323 }
324 }