1 10 package mondrian.rolap; 11 12 import mondrian.olap.Util; 13 14 import javax.sql.DataSource ; 15 import java.sql.Connection ; 16 import java.sql.ResultSet ; 17 import java.sql.SQLException ; 18 import java.sql.Statement ; 19 import java.io.PrintWriter ; 20 21 51 public class SqlStatement { 52 private final DataSource dataSource; 53 private Connection jdbcConnection; 54 private ResultSet resultSet; 55 private final String sql; 56 private final int maxRows; 57 private final String component; 58 private final int resultSetType; 59 private final int resultSetConcurrency; 60 private final RolapUtil.Semaphore querySemaphore = RolapUtil 61 .getQuerySemaphore(); 62 private final String message; 63 private boolean haveSemaphore; 64 public int rowCount; 65 private long startTime; 66 private final PrintWriter trace; 67 68 SqlStatement( 69 DataSource dataSource, 70 String sql, 71 int maxRows, 72 String component, 73 String message, 74 int resultSetType, 75 int resultSetConcurrency) 76 { 77 this.dataSource = dataSource; 78 this.sql = sql; 79 this.maxRows = maxRows; 80 this.component = component; 81 this.message = message; 82 this.resultSetType = resultSetType; 83 this.resultSetConcurrency = resultSetConcurrency; 84 this.trace = RolapUtil.checkTracing(); 85 } 86 87 public void execute() throws SQLException { 88 this.jdbcConnection = dataSource.getConnection(); 89 querySemaphore.enter(); 90 haveSemaphore = true; 91 Statement statement = null; 92 String status = "failed"; 93 94 if (trace != null) { 96 trace.print(component + ": executing sql ["); 97 if (sql.indexOf('\n') >= 0) { 98 trace.println(); 101 } 102 trace.print(sql); 103 trace.print(']'); 104 trace.flush(); 105 } 106 107 RolapUtil.ExecuteQueryHook hook = RolapUtil.threadHooks.get(); 109 if (hook != null) { 110 hook.onExecuteQuery(sql); 111 } 112 try { 113 startTime = System.currentTimeMillis(); 114 if (resultSetType < 0 || resultSetConcurrency < 0) { 115 statement = jdbcConnection.createStatement(); 116 } else { 117 statement = jdbcConnection.createStatement( 118 resultSetType, 119 resultSetConcurrency); 120 } 121 if (maxRows > 0) { 122 statement.setMaxRows(maxRows); 123 } 124 this.resultSet = statement.executeQuery(sql); 125 long time = System.currentTimeMillis(); 126 final long execMs = time - startTime; 127 Util.addDatabaseTime(execMs); 128 status = ", exec " + execMs + " ms"; 129 } catch (SQLException e) { 130 status = ", failed (" + e + ")"; 131 try { 132 if (statement != null) { 133 statement.close(); 134 } 135 } catch (SQLException e2) { 136 } 138 throw handle(e); 139 } finally { 140 if (trace != null) { 141 trace.print(status); 142 } 143 if (RolapUtil.LOGGER.isDebugEnabled()) { 144 RolapUtil.LOGGER.debug(component + ": executing sql [" + 145 sql + "]" + status); 146 } 147 } 148 } 149 150 160 public void close() { 161 if (haveSemaphore) { 162 haveSemaphore = false; 163 querySemaphore.leave(); 164 } 165 if (resultSet != null) { 166 try { 167 resultSet.close(); 168 } catch (SQLException e) { 169 throw Util.newError(message + "; sql=[" + sql + "]"); 170 } finally { 171 resultSet = null; 172 } 173 } 174 if (jdbcConnection != null) { 175 try { 176 jdbcConnection.close(); 177 } catch (SQLException e) { 178 throw Util.newError(message + "; sql=[" + sql + "]"); 179 } finally { 180 jdbcConnection = null; 181 } 182 } 183 long time = System.currentTimeMillis(); 184 long totalMs = time - startTime; 185 if (trace != null) { 186 trace.println( 187 ", exec+fetch " + totalMs + " ms, " + rowCount + " rows"); 188 } 189 } 190 191 public ResultSet getResultSet() { 192 return resultSet; 193 } 194 195 203 public RuntimeException handle(Exception e) { 204 RuntimeException runtimeException = 205 Util.newError(e, message + "; sql=[" + sql + "]"); 206 try { 207 close(); 208 } catch (RuntimeException re) { 209 } 211 return runtimeException; 212 } 213 } 214 215 | Popular Tags |