1 21 22 package org.opensubsystems.core.persist.db.hsqldb; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.Map ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 32 import org.opensubsystems.core.data.BasicDataObject; 33 import org.opensubsystems.core.data.ModifiableDataObject; 34 import org.opensubsystems.core.error.OSSDatabaseAccessException; 35 import org.opensubsystems.core.error.OSSException; 36 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 37 import org.opensubsystems.core.persist.db.DatabaseImpl; 38 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 39 import org.opensubsystems.core.util.DatabaseUtils; 40 import org.opensubsystems.core.util.Log; 41 42 50 public class HsqlDBDatabaseImpl extends DatabaseImpl 51 { 52 54 57 private static Logger s_logger = Log.getInstance(HsqlDBDatabaseImpl.class); 58 59 61 66 public HsqlDBDatabaseImpl( 67 ) throws OSSException 68 { 69 super(); 70 } 71 72 75 public void stop( 76 ) throws OSSException 77 { 78 if (m_bDatabaseStarted) 83 { 84 Connection cntAdminDBConnection = null; 85 boolean bStopped = false; 86 87 try 88 { 89 cntAdminDBConnection = getAdminConnection(true); 92 93 if (cntAdminDBConnection == null) 94 { 95 s_logger.log(Level.WARNING, 96 "Unable to connect to database as admin to stop it."); 97 } 98 else 99 { 100 Statement stmQuery = null; 101 try 102 { 103 stmQuery = cntAdminDBConnection.createStatement(); 104 if (stmQuery.execute("shutdown")) 105 { 106 } 109 bStopped = true; 110 s_logger.log(Level.FINER, "Database is stopped."); 111 } 112 catch (SQLException sqleExc) 113 { 114 s_logger.log(Level.WARNING, "Unable to stop the database.", sqleExc); 115 } 116 finally 117 { 118 if ((!bStopped) && (stmQuery != null)) 119 { 120 try 122 { 123 stmQuery.close(); 124 } 125 catch (SQLException sqleExc) 126 { 127 s_logger.log(Level.WARNING, 128 "Closing of statement has failed.", 129 sqleExc); 130 } 131 } 132 } 133 } 134 } 135 finally 136 { 137 try 138 { 139 if (cntAdminDBConnection != null) 140 { 141 DatabaseConnectionFactoryImpl.getInstance().returnConnection( 144 cntAdminDBConnection); 145 } 146 } 147 finally 148 { 149 super.stop(); 150 } 151 } 152 } 153 } 154 155 158 protected void createUser( 159 Connection cntAdminDBConnection 160 ) throws OSSException 161 { 162 PreparedStatement pstmQuery = null; 164 try 165 { 166 177 178 StringBuffer buffer = new StringBuffer (); 179 180 buffer.append("CREATE user "); 181 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser()); 182 buffer.append(" PASSWORD "); 183 buffer.append(DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword()); 184 buffer.append(" ADMIN "); 189 pstmQuery = cntAdminDBConnection.prepareStatement(buffer.toString()); 190 191 if (pstmQuery.execute()) 192 { 193 pstmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 195 } 196 197 DatabaseTransactionFactoryImpl.getInstance().commitTransaction( 202 cntAdminDBConnection); 203 204 s_logger.log(Level.FINER, "Database user " 205 + DatabaseConnectionFactoryImpl.getInstance().getDatabaseUser() 206 + " with password " 207 + DatabaseConnectionFactoryImpl.getInstance().getDatabasePassword() 208 + " created."); 209 } 210 catch (SQLException sqleExc) 211 { 212 try 213 { 214 DatabaseTransactionFactoryImpl.getInstance().rollbackTransaction( 219 cntAdminDBConnection); 220 } 221 catch (SQLException sqleExc2) 222 { 223 s_logger.log(Level.WARNING, 225 "Failed to rollback changes for creation of user.", 226 sqleExc2); 227 } 228 s_logger.log(Level.SEVERE, 229 "Unable to create default database user.", 230 sqleExc); 231 throw new OSSDatabaseAccessException("Unable to create default database user.", 232 sqleExc); 233 } 234 finally 235 { 236 DatabaseUtils.closeStatement(pstmQuery); 237 } 238 } 239 240 243 public void startDatabaseServer( 244 ) throws OSSException 245 { 246 } 249 250 253 public void createDatabaseInstance( 254 ) throws OSSException 255 { 256 } 259 260 263 public int getDatabaseType() 264 { 265 return DatabaseImpl.HSQLDB_DATABASE_TYPE; 266 } 267 268 271 public String getDatabaseTypeIdentifier() 272 { 273 return DatabaseImpl.HSQLDB_DATABASE_TYPE_IDENTIFIER; 274 } 275 276 279 public String getCurrentTimestampFunctionCall() 280 { 281 return "now"; 283 } 284 285 288 public String getSQLCountFunctionCall() 289 { 290 return "count(*)"; 292 } 293 294 297 public Object [] getSQLAnalyzeFunctionCall( 298 Map mapTableNames 299 ) 300 { 301 return null; 304 } 305 306 309 public int getTransactionIsolation( 310 int iTransactionIsolation 311 ) 312 { 313 return -1; 316 } 317 318 321 public boolean isCallableStatement( 322 String strQuery 323 ) 324 { 325 return false; 327 } 328 329 332 public void insertAndFetchGeneratedValues( 333 Connection dbConnection, 334 PreparedStatement insertStatement, 335 boolean bIsInDomain, 336 String strTableName, 337 int iIndex, 338 BasicDataObject data 339 ) throws SQLException , 340 OSSException 341 { 342 HsqlDBDataUtils.insertAndFetchGeneratedValues(dbConnection, 343 insertStatement, bIsInDomain, strTableName, data); 344 } 345 346 349 public void updatedAndFetchGeneratedValues( 350 String strDataName, 351 Connection dbConnection, 352 PreparedStatement updateStatement, 353 boolean bIsInDomain, 354 String strTableName, 355 int iIndex, 356 ModifiableDataObject data 357 ) throws SQLException , 358 OSSException 359 { 360 HsqlDBDataUtils.updatedAndFetchGeneratedValues(strDataName, dbConnection, 361 updateStatement, bIsInDomain, strTableName, data); 362 } 363 } 364 | Popular Tags |