1 21 22 package org.opensubsystems.core.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.util.Properties ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import javax.transaction.Status ; 31 import javax.transaction.SystemException ; 32 import javax.transaction.UserTransaction ; 33 34 import org.opensubsystems.core.error.OSSDatabaseAccessException; 35 import org.opensubsystems.core.error.OSSDynamicClassException; 36 import org.opensubsystems.core.error.OSSException; 37 import org.opensubsystems.core.persist.db.transaction.J2EETransactionFactoryImpl; 38 import org.opensubsystems.core.persist.db.transaction.SimpleLocalTransactionFactoryImpl; 39 import org.opensubsystems.core.util.ClassFactory; 40 import org.opensubsystems.core.util.Config; 41 import org.opensubsystems.core.util.GlobalConstants; 42 import org.opensubsystems.core.util.J2EEUtils; 43 import org.opensubsystems.core.util.Log; 44 import org.opensubsystems.core.util.TransactionFactory; 45 46 54 public abstract class DatabaseTransactionFactoryImpl implements DatabaseTransactionFactory 55 { 56 58 63 public static final String TRANSACTION_TIMEOUT = "oss.transaction.timeout"; 64 65 71 public static final String TRANSACTION_MONITOR = "oss.transaction.monitor"; 72 73 75 78 private static final String IMPL_LOCK = "IMPL_LOCK"; 79 80 85 public static final int TRANSACTION_TIMEOUT_DEFAULT = 600; 86 87 91 public static final boolean TRANSACTION_MONITOR_DEFAULT = false; 92 93 95 98 private static Logger s_logger = Log.getInstance(DatabaseTransactionFactoryImpl.class); 99 100 103 private static DatabaseTransactionFactory s_defaultInstance = null; 104 105 110 protected static int s_iTransactionTimeout; 111 112 117 protected static boolean s_bTransactionMonitor; 118 119 121 124 static 125 { 126 Properties prpSettings; 128 129 prpSettings = Config.getInstance().getPropertiesSafely(); 130 s_iTransactionTimeout = Config.getIntPropertyInRange( 131 prpSettings, 132 TRANSACTION_TIMEOUT, 133 TRANSACTION_TIMEOUT_DEFAULT, 134 "Default transaction timout", 135 0, Integer.MAX_VALUE); 137 138 s_bTransactionMonitor = Config.getBooleanProperty( 139 prpSettings, 140 TRANSACTION_MONITOR, 141 TRANSACTION_MONITOR_DEFAULT, 142 "Default transaction monitoring"); 143 } 144 145 148 public DatabaseTransactionFactoryImpl( 149 ) 150 { 151 super(); 152 } 153 154 156 165 public static DatabaseTransactionFactory getInstance( 166 ) throws OSSException 167 { 168 if (s_defaultInstance == null) 169 { 170 synchronized (IMPL_LOCK) 173 { 174 if (s_defaultInstance == null) 175 { 176 DatabaseTransactionFactory transactionFactory = null; 177 178 try 179 { 180 transactionFactory = (DatabaseTransactionFactory)ClassFactory.getInstance() 181 .createInstance(DatabaseTransactionFactory.class); 182 } 183 catch (OSSDynamicClassException dneExc) 184 { 185 Class defaultTransactionFactoryImpl = SimpleLocalTransactionFactoryImpl.class; 188 189 if (J2EEUtils.getJ2EEServerType() != J2EEUtils.J2EE_SERVER_NO) 193 { 194 defaultTransactionFactoryImpl = J2EETransactionFactoryImpl.class; 195 } 196 197 transactionFactory = (DatabaseTransactionFactory)ClassFactory.getInstance() 198 .createInstance(TransactionFactory.class, 199 defaultTransactionFactoryImpl); 200 } 201 202 setInstance(transactionFactory); 203 } 204 } 205 } 206 207 return s_defaultInstance; 208 } 209 210 217 public static void setInstance( 218 DatabaseTransactionFactory defaultFactory 219 ) 220 { 221 if (GlobalConstants.ERROR_CHECKING) 222 { 223 224 assert defaultFactory != null 225 : "Default transaction factory instance cannot be null"; 226 } 227 228 synchronized (IMPL_LOCK) 229 { 230 s_defaultInstance = defaultFactory; 231 s_logger.fine("Default database transaction factory is " 232 + s_defaultInstance.getClass().getName()); 233 } 234 } 235 236 239 public void commitTransaction( 240 Connection cntConnection 241 ) throws SQLException  242 { 243 try 244 { 245 boolean bAutoCommit = cntConnection.getAutoCommit(); 246 247 if ((!isTransactionInProgress()) && (!bAutoCommit)) 248 { 249 if (s_bTransactionMonitor) 253 { 254 s_logger.finest("commitTransaction on connection " 255 + cntConnection.toString()); 256 } 257 cntConnection.commit(); 258 if (s_bTransactionMonitor) 259 { 260 s_logger.finest("commitTransaction sucessfull on connection " 261 + cntConnection.toString()); 262 } 263 } 264 else 265 { 266 if (s_bTransactionMonitor) 267 { 268 s_logger.finest("commitTransaction on connection " 269 + cntConnection.toString() 270 + " ignored since either transaction in progress" 271 + " or autoCommit is set. Autocommit value is " 272 + bAutoCommit); 273 } 274 } 275 } 276 catch (SystemException seExc) 277 { 278 SQLException sqlExc = new SQLException ("Cannot check state of the transaction"); 279 sqlExc.initCause(seExc); 280 throw sqlExc; 281 } 282 catch (OSSException osseExc) 283 { 284 SQLException sqlExc = new SQLException ("Cannot check state of the transaction"); 285 sqlExc.initCause(osseExc); 286 throw sqlExc; 287 } 288 } 289 290 293 public void rollbackTransaction( 294 Connection cntConnection 295 ) throws SQLException  296 { 297 try 298 { 299 boolean bAutoCommit = cntConnection.getAutoCommit(); 300 301 if (cntConnection != null) 304 { 305 if ((!isTransactionInProgress()) && (!bAutoCommit)) 306 { 307 if (s_bTransactionMonitor) 311 { 312 s_logger.finest("rollbackTransaction on connection " 313 + cntConnection.toString()); 314 } 315 cntConnection.rollback(); 316 if (s_bTransactionMonitor) 317 { 318 s_logger.finest("rollbackTransaction sucessfull on connection " 319 + cntConnection.toString()); 320 } 321 } 322 else 323 { 324 if (s_bTransactionMonitor) 325 { 326 s_logger.finest("rollbackTransaction on connection " 327 + cntConnection.toString() 328 + " ignored since either transaction in progress" 329 + " or autoCommit is set. Autocommit value is " 330 + bAutoCommit); 331 } 332 } 333 } 334 } 335 catch (SystemException seExc) 336 { 337 s_logger.log(Level.WARNING, "Cannot check state of the transaction", 338 seExc); 339 cntConnection.rollback(); 341 } 342 catch (OSSException osseExc) 343 { 344 SQLException sqlExc = new SQLException ("Cannot check state of the transaction"); 345 sqlExc.initCause(osseExc); 346 throw sqlExc; 347 } 348 } 349 350 357 public boolean isTransactionInProgress( 358 ) throws SystemException , 359 OSSException 360 { 361 UserTransaction transaction = requestTransaction(); 362 int iStatus = Status.STATUS_NO_TRANSACTION; 363 364 if (transaction != null) 365 { 366 iStatus = transaction.getStatus(); 369 } 370 371 return ((iStatus != Status.STATUS_NO_TRANSACTION) 372 && (iStatus != Status.STATUS_COMMITTED) 373 && (iStatus != Status.STATUS_ROLLEDBACK)); 374 } 375 376 383 public boolean isTransactionMonitored() 384 { 385 return s_bTransactionMonitor; 386 } 387 388 390 417 protected Connection requestTransactionalConnection( 418 boolean bAutoCommit, 419 String strDataSourceName, 420 String strUser, 421 String strPassword, 422 DatabaseConnectionFactoryImpl connectionFactory 423 ) throws OSSDatabaseAccessException 424 { 425 Connection realConnection; 429 430 if (strDataSourceName == null) 431 { 432 if ((strUser == null) && (strPassword == null)) 433 { 434 realConnection = connectionFactory.requestNonTransactionalConnection( 436 bAutoCommit); 437 } 438 else 439 { 440 realConnection = connectionFactory.requestNonTransactionalConnection( 441 bAutoCommit, strUser, strPassword); 442 } 443 } 444 else 445 { 446 if ((strUser == null) && (strPassword == null)) 447 { 448 realConnection = connectionFactory.requestNonTransactionalConnection( 450 bAutoCommit, strDataSourceName); 451 } 452 else 453 { 454 realConnection = connectionFactory.requestNonTransactionalConnection( 455 bAutoCommit, strDataSourceName, strUser, strPassword); 456 } 457 } 458 459 return realConnection; 460 } 461 462 474 protected void returnTransactionalConnection( 475 Connection cntDBConnection, 476 DatabaseConnectionFactoryImpl connectionFactory 477 ) 478 { 479 connectionFactory.returnNonTransactionalConnection(cntDBConnection); 483 } 484 } 485
| Popular Tags
|