1 23 package com.lutris.appserver.server.sql.standard; 24 25 import java.lang.reflect.Constructor ; 26 import java.sql.Driver ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.util.Date ; 30 31 import org.enhydra.dods.Common; 32 import org.enhydra.dods.CommonConstants; 33 import org.enhydra.dods.DODS; 34 import org.enhydra.dods.DriverDependencies; 35 import org.enhydra.dods.cache.CacheConstants; 36 37 import com.lutris.appserver.server.sql.AbstractDBTransactionFactory; 38 import com.lutris.appserver.server.sql.ConnectionAllocator; 39 import com.lutris.appserver.server.sql.DBConnection; 40 import com.lutris.appserver.server.sql.DBQuery; 41 import com.lutris.appserver.server.sql.DBTransaction; 42 import com.lutris.appserver.server.sql.DBTransactionFactoryCreator; 43 import com.lutris.appserver.server.sql.DatabaseManagerConfiguration; 44 import com.lutris.appserver.server.sql.ExtendedDBConnection; 45 import com.lutris.appserver.server.sql.LogicalDatabase; 46 import com.lutris.appserver.server.sql.ObjectId; 47 import com.lutris.appserver.server.sql.ObjectIdAllocator; 48 import com.lutris.appserver.server.sql.ObjectIdException; 49 import com.lutris.dods.builder.generator.query.RDBColumn; 50 import com.lutris.logging.Logger; 51 import com.lutris.util.Config; 52 import com.lutris.util.ConfigException; 53 import com.lutris.util.KeywordValueException; 54 84 public class StandardLogicalDatabase implements LogicalDatabase, CacheConstants, DriverSpecificConstants { 85 86 public static final String PARAMNAME_DRIV_DEP_CLASS = "DriverDependenciesClass"; 87 88 91 protected AbstractDBTransactionFactory transactionFactory; 92 93 96 protected ConnectionAllocator connectionAllocator; 97 98 101 protected ObjectIdAllocator objectIdAllocator; 102 103 104 105 110 protected Driver driver; 111 112 115 protected String driverClassName; 116 117 118 121 protected String dbName; 122 123 126 protected String dbType; 127 128 131 private DatabaseConfiguration dbConf; 132 private DriverDependencies drivDepsInstance; 133 134 139 public StandardLogicalDatabase() {} 140 141 154 public StandardLogicalDatabase(String dbName, Config dbConfig) 155 throws ConfigException, SQLException { 156 this.init(dbName, dbConfig); 157 } 158 159 174 public StandardLogicalDatabase(String dbName, Config dbConfig, DatabaseManagerConfiguration DbManagerConf) 175 throws ConfigException, SQLException { 176 this.init(dbName, dbConfig, DbManagerConf); 177 } 178 179 180 public void init(String dbName, Config dbConfig) 181 throws ConfigException, SQLException { 182 dbConf = new DatabaseConfiguration(dbName); 183 initConf( dbName, dbConfig); 184 185 } 186 187 public void init(String dbName, Config dbConfig, DatabaseManagerConfiguration DbManagerConf) 188 throws ConfigException, SQLException { 189 dbConf = new DatabaseConfiguration(dbName, DbManagerConf); 190 initConf( dbName, dbConfig); 191 192 } 193 194 195 206 private void initConf(String dbName, Config dbConfig) 207 throws ConfigException, SQLException { 208 209 this.dbName = dbName; 210 211 212 Config objIdConfig, connectionConfig; 214 215 try { 216 String changeAutocommit = dbConfig.getString("ChangeAutocommit", "true"); 217 if (changeAutocommit.equalsIgnoreCase("false")){ 218 Common.setChangeAutocommit(dbName, false); 219 }else if (changeAutocommit.equalsIgnoreCase("true")){ 220 Common.setChangeAutocommit(dbName, true); 221 }else{ 222 throw new ConfigException("invalid value for ChangeAutocommit parameter"); 223 } 224 } catch (ConfigException e) { 225 throw new ConfigException("Error reading ChangeAutocommit : "+e.getMessage()); 226 } 227 228 try { 229 objIdConfig = (Config) dbConfig.getSection("ObjectId"); 230 connectionConfig = (Config) dbConfig.getSection("Connection"); 231 } catch (KeywordValueException except) { 232 throw new ConfigException("Invalid ObjectId or Connection sections in config file."); 233 } 234 235 Object dataSource; 236 try { 237 dataSource = connectionConfig.getDataSource("DataSourceName"); 238 } 239 catch (Exception ex){ 240 dataSource = null; 241 } 242 if (dataSource==null){ 243 this.driverClassName = dbConfig.getString("JdbcDriver"); 244 String jdbcDriver = this.driverClassName ; 245 246 try { 249 Class driverClass = Class.forName(jdbcDriver); 251 252 driver = (Driver ) driverClass.newInstance(); 253 } catch (java.lang.ClassNotFoundException except) { 254 throw new SQLException ("can't load JDBC driver class: " 255 + except.getMessage()); 256 } catch (java.lang.InstantiationException except) { 257 throw new SQLException ("can't instantiate JDBC driver class: " 258 + except.getMessage()); 259 } catch (java.lang.IllegalAccessException except) { 260 throw new SQLException ("can't instantiate JDBC driver class: " 261 + except.getMessage()); 262 } 263 try { 264 this.dbType = dbConfig.getString("ClassType"); 265 }catch(Exception e1){ 266 this.dbType = Common.getDatabaseVendor(jdbcDriver); 267 } 268 } 269 270 try { 271 dbConf.setDBConnectionFactoryName(dbConfig.getString(CommonConstants.CONNECTION_FACTORY)); 272 } catch (Exception ex) {} 273 274 try { 275 dbConf.setConnectionAllocatorName(dbConfig.getString(CommonConstants.CONNECTION_ALLOCATOR)); 276 } catch (Exception ex) {} 277 278 ConnectionAllocator connectionAllocator = loadConnectionAllocator(connectionConfig); 279 ObjectIdAllocator objectIdAllocator = loadObjectIdAllocator(objIdConfig); 280 281 this.connectionAllocator = connectionAllocator; 282 this.objectIdAllocator = objectIdAllocator; 283 if(dataSource!=null){ 284 String databaseType=null; 285 String fullDriverClassName=null; 286 try{ 287 ExtendedDBConnection tempConnection = (ExtendedDBConnection)connectionAllocator.allocate(); 288 String driverName=tempConnection.getConnection().getMetaData().getDriverName(); 289 tempConnection.release(); 290 databaseType=Common.getDatabaseVendorFromDriverName(driverName); 291 fullDriverClassName=Common.getDatabaseDriverClassFromDriverName(driverName); 292 }catch(Exception e){ 293 try { 294 databaseType = dbConfig.getString("ClassType"); 295 fullDriverClassName = dbConfig.getString("JdbcDriver",null); 296 }catch(Exception e1){ 297 databaseType = Common.getDatabaseVendor(dbConfig.getString("JdbcDriver",null)); 298 } 299 } 300 this.dbType=databaseType; 301 this.driverClassName = fullDriverClassName; 302 } 303 304 this.initDisableFetchSizeWithMaxRows(); 305 this.initResultSetConcurrency(); 306 this.initResultSetType(); 307 308 309 try { 310 dbConf.setDBTransactionFactoryName(dbConfig.getString(CommonConstants.TRANSACTION_FACTORY)); 311 } catch (Exception ex) {} 312 transactionFactory = DBTransactionFactoryCreator.getDBTransactionFactory(dbConf.getDBTransactionFactoryName(),this); 314 Config defaultsConfig = dbConfig; 318 319 if (defaultsConfig != null) { 320 try { 321 dbConf.setLazyLoading(defaultsConfig.getBoolean(PARAMNAME_LAZY_LOADING)); 322 } catch (Exception ex) {} 323 try { 324 dbConf.setCaseSensitive(defaultsConfig.getBoolean(PARAMNAME_CASE_SENSITIVE)); 325 } catch (Exception ex) {} 326 327 try { 328 dbConf.setMaxExecuteTime(defaultsConfig.getInt(PARAMNAME_MAX_EXECUTE_TIME)); 329 } catch (Exception ex) {} 330 try { 331 dbConf.setTransactionCheck(defaultsConfig.getBoolean(PARAMNAME_TRANSACTION_CHECK)); 332 } catch (Exception ex) {} 333 try { 334 dbConf.setDeleteCheckVersion(defaultsConfig.getBoolean(PARAMNAME_DELETE_CHECK_VERSION)); 335 } catch (Exception ex) {} 336 try { 337 dbConf.setAutoSave(defaultsConfig.getBoolean(PARAMNAME_AUTO_SAVE)); 338 } catch (Exception ex) {} 339 try { 340 dbConf.setAutoSaveCreateVirgin(defaultsConfig.getBoolean(PARAMNAME_AUTO_SAVE_CREATE_VIRGIN)); 341 } catch (Exception ex) {} 342 try { 343 dbConf.setAutoWrite(defaultsConfig.getBoolean(PARAMNAME_AUTO_WRITE)); 344 } catch (Exception ex) {} 345 try { 346 dbConf.setTransactionCaches(defaultsConfig.getBoolean(PARAMNAME_TRANSACTION_CACHES)); 347 } catch (Exception ex) {} 348 try { 349 dbConf.setDeadlockWaitTime(defaultsConfig.getInt(PARAMNAME_DEADLOCK_READ_TIME)); 350 } catch (Exception ex) {} 351 try { 352 dbConf.setDeadlockRetryCount(defaultsConfig.getInt(PARAMNAME_DEADLOCK_RETRY_NUMBER)); 353 } catch (Exception ex) {} 354 try { 355 dbConf.setReadOnly(defaultsConfig.getBoolean(PARAMNAME_ALL_READ_ONLY)); 356 } catch (Exception ex) {} 357 try { 358 dbConf.setDefaultFetchSize(defaultsConfig.getInt(PARAMNAME_DEFAULT_FETCH_SIZE)); 359 } catch (Exception ex) {} 360 try { 361 dbConf.setQueryTimeout(defaultsConfig.getInt(PARAMNAME_QUERY_TIMEOUT)); 362 } catch (Exception ex) {} 363 try { 364 dbConf.setSelectOids(defaultsConfig.getBoolean(PARAMNAME_SELECT_OIDS)); 365 } catch (Exception ex) {} 366 try { 367 dbConf.setIncrementVersions(defaultsConfig.getBoolean(PARAMNAME_INCREMENT_VERSIONS)); 368 } catch (Exception ex) {} 369 try { 370 boolean tmpSCrN = defaultsConfig.getBoolean(PARAMNAME_USE_CURSOR_NAME); 371 dbConf.setUseCursorName(tmpSCrN); 372 } catch (Exception ex) {} 373 try { 374 dbConf.setFullCacheCountLimit(defaultsConfig.getInt(CacheConstants.FULL_CACHE_COUNT_LIMIT)); 375 } catch (Exception ex) {} 376 try { 377 dbConf.setQueryCacheImplClass(defaultsConfig.getString(CommonConstants.QUERY_CACAHE_IMPL_CLASS)); 378 } catch (Exception ex) {} 379 try { 380 dbConf.setInitCachesResultSetType(defaultsConfig.getString(CommonConstants.INIT_CACHES_RESULT_SET_TYPE)); 381 } catch (Exception ex) {} 382 try { 383 dbConf.setInitCachesResultSetConcurrency(defaultsConfig.getString(CommonConstants.INIT_CACHES_RESULT_SET_CONCURRENCY)); 384 } catch (Exception ex) {} 385 try { 386 dbConf.setSqlBatch(defaultsConfig.getBoolean(CommonConstants.SQL_BATCH)); 387 } catch (Exception ex) {} 388 try { 389 dbConf.setQueryTimeLimit(new Integer (defaultsConfig.getInt(CommonConstants.QUERY_TIME_LIMIT))); 390 } catch (Exception ex) {} 391 392 394 try { 395 dbConf.setXaDefaultTimeout(defaultsConfig.getInt(CommonConstants.XA_DEFAULT_TIMEOUT)); 396 } catch (Exception ex) {} 397 try { 398 dbConf.setXaTransactonManagerLookupName(defaultsConfig.getString(CommonConstants.XA_TM_LOOKUP_NAME)); 399 400 } catch (Exception ex) {} 401 try { 402 dbConf.setXaUsageCase(defaultsConfig.getInt(CommonConstants.XA_USAGE_CASE)); 403 } catch (Exception ex) {} 404 try { 405 dbConf.setXaWrappedTransImplFactory(defaultsConfig.getString(CommonConstants.XA_WRAPPED_TRANS_IMPL_FACTORY)); 406 } catch (Exception ex) {} 407 try { 408 dbConf.setXaUserTransactonLookupName(defaultsConfig.getString(CommonConstants.XA_USER_TRANSACTION_LOOKUP_NAME)); 409 } catch (Exception e) { } 410 try { 411 dbConf.setXaJtaSupport(defaultsConfig.getString(CommonConstants.XA_JTA_SUPPORT)); 412 } catch (Exception e) { } 413 414 } 416 try { 417 defaultsConfig = (Config) dbConfig.getSection("cache"); 418 } catch (KeywordValueException except) { 419 throw new ConfigException("No DatabaseManager.defaults defined in config file."); 420 } 421 if (defaultsConfig != null) { 422 try { 423 dbConf.setReserveFactor(defaultsConfig.getDouble(PARAMNAME_RESERVE_FACTOR)); 424 } catch (Exception ex) {} 425 try { 426 dbConf.setCachePercentage(defaultsConfig.getDouble(PARAMNAME_CACHE_PERCENTAGE)); 427 } catch (Exception ex) {} 428 try { 429 dbConf.setMaxCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_CACHE_SIZE)); 430 } catch (Exception ex) {} 431 try { 432 dbConf.setMaxSimpleCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_SIMPLE_CACHE_SIZE)); 433 } catch (Exception ex) {} 434 try { 435 dbConf.setMaxComplexCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_COMPLEX_CACHE_SIZE)); 436 } catch (Exception ex) {} 437 try { 438 dbConf.setMaxMultiJoinCacheSize(defaultsConfig.getInt(PARAMNAME_MAX_MULTI_JOIN_CACHE_SIZE)); 439 } catch (Exception ex) {} 440 try { 441 dbConf.setInitialCacheFetchSize(defaultsConfig.getInt(PARAMNAME_INITIAL_CACHE_FETCH_SIZE)); 442 } catch (Exception ex) {} 443 try { 444 dbConf.setInitialDSCacheSize(defaultsConfig.getInt(PARAMNAME_INITIAL_DS_CACHE_SIZE)); 445 } catch (Exception ex) {} 446 try { 447 dbConf.setDodsCacheFactory(defaultsConfig.getString(PARAMNAME_DODS_CACHE_FACTORY)); 448 } catch (Exception ex) {} 449 450 451 } 452 453 } 454 455 463 public void checkOId(ObjectId oid) throws ObjectIdException { 464 objectIdAllocator.checkOId(oid); 465 } 466 467 477 public ConnectionAllocator loadConnectionAllocator(Config connectionConfig) 478 throws ConfigException { 479 Class connectionAllocatorClass = null; 480 Constructor connectionAllocatorConstructor = null; 481 Class [] methodTypes={LogicalDatabase.class,connectionConfig.getClass()}; 482 Object [] methodArgs={this,connectionConfig}; 483 ConnectionAllocator allocator = null; 484 if (dbConf.getConnectionAllocatorName()!=null){ 485 try{ 486 connectionAllocatorClass = Class.forName(dbConf.getConnectionAllocatorName()); 487 connectionAllocatorConstructor = connectionAllocatorClass.getConstructor(methodTypes); 488 allocator = (ConnectionAllocator)connectionAllocatorConstructor.newInstance(methodArgs); 489 }catch(Exception e){ 490 DODS.getLogChannel().write(Logger.ERROR,"Failed to make Connection Allocator :"+dbConf.getConnectionAllocatorName()+" creating StandardConnectionAllocator insted"); 491 allocator = null; 492 } 493 } 494 if (dbConf.getConnectionAllocatorName()==null || allocator == null){ 495 try{ 497 connectionAllocatorClass = Class.forName("com.lutris.appserver.server.sql.standard.StandardConnectionAllocator"); 498 connectionAllocatorConstructor = connectionAllocatorClass.getConstructor(methodTypes); 499 allocator = (ConnectionAllocator)connectionAllocatorConstructor.newInstance(methodArgs); 500 }catch(Exception e){ 501 String str = "Failed to make Standard Connection Allocator : com.lutris.appserver.server.sql.standard.StandardConnectionAllocator"; 502 DODS.getLogChannel().write(Logger.CRITICAL,str); 503 throw new Error (str); 504 } 505 } 506 return allocator; 507 } 508 509 519 public ObjectIdAllocator loadObjectIdAllocator(Config objIdConfig) 520 throws ConfigException { 521 return new StandardObjectIdAllocator(this, objIdConfig); 522 } 523 524 533 public DBConnection allocateConnection() 534 throws SQLException { 535 return connectionAllocator.allocate(); 536 } 537 538 546 public ObjectId allocateObjectId() 547 throws ObjectIdException { 548 return objectIdAllocator.allocate(); 549 } 550 551 558 public DBTransaction createTransaction() 559 throws SQLException { 560 return transactionFactory.getTransaction(connectionAllocator.allocate()); 561 } 562 563 570 public DBQuery createQuery() 571 throws SQLException { 572 return new StandardDBQuery(connectionAllocator.allocate()); 573 } 574 575 579 public void shutdown() { 580 connectionAllocator.dropAllNow(); 581 driver = null; 582 } 583 584 589 public String getName() { 590 return dbName; 591 } 592 593 598 public String getType() { 599 return dbType; 600 } 601 602 607 public String getDriverClassName() { 608 return this.driverClassName; 609 } 610 611 612 613 619 public String getDriverProperty(String paramName) { 620 String key ="Drivers/"+driverClassName+"/"+paramName; 621 return Common.getDodsConfProperty(key,dbType); 622 } 623 624 625 626 627 632 public int getActiveConnectionCount() { 633 return connectionAllocator.getActiveCount(); 634 } 635 636 641 public int getMaxConnectionCount() { 642 return connectionAllocator.getMaxCount(); 643 } 644 645 651 public Date getMaxConnectionCountDate() { 652 return connectionAllocator.getMaxCountDate(); 653 } 654 655 658 public void resetMaxConnectionCount() { 659 connectionAllocator.resetMaxCount(); 660 } 661 662 667 public long getRequestCount() { 668 return connectionAllocator.getRequestCount(); 669 } 670 671 676 public DatabaseConfiguration getDatabaseConfiguration() { 677 return dbConf; 678 } 679 680 681 685 686 public boolean getDisableFetchSizeWithMaxRows(){ 687 return dbConf.getDisableFetchSizeWithMaxRows(); 688 } 689 690 694 private void initDisableFetchSizeWithMaxRows() { 695 String tmpDFSWMR = getDriverProperty(PARAMNAME_DISABLE_FETCH_SIZE_WITH_MAX_ROWS); 696 if (tmpDFSWMR==null){ 697 dbConf.setDisableFetchSizeWithMaxRows(DEFAULT_DISABLE_FETCH_SIZE_WITH_MAX_ROWS); 698 }else{ 699 if (tmpDFSWMR.equalsIgnoreCase("true")){ 700 dbConf.setDisableFetchSizeWithMaxRows(true); 701 }else if (tmpDFSWMR.equalsIgnoreCase("false")){ 702 dbConf.setDisableFetchSizeWithMaxRows(false); 703 }else{ 704 DODS.getLogChannel().write(Logger.DEBUG,"Invalid value for DisableFetchSizeWithMaxRows parameter :"+tmpDFSWMR); 705 dbConf.setDisableFetchSizeWithMaxRows(DEFAULT_DISABLE_FETCH_SIZE_WITH_MAX_ROWS); 706 } 707 } 708 } 709 710 711 715 public boolean getUseCursorName() { 716 if (dbConf.getUseCursorName()==null){ 717 String tmpUCrN = getDriverProperty(PARAMNAME_USE_CURSOR_NAME); 718 if (tmpUCrN==null){ 719 return DEFAULT_USE_CURSOR_NAME; 720 }else{ 721 if (tmpUCrN.equalsIgnoreCase("true")) { 722 setUseCursorName(true); 723 return true; 724 }else if (tmpUCrN.equalsIgnoreCase("false")){ 725 setUseCursorName(false); 726 return false; 727 }else{ 728 DODS.getLogChannel().write(Logger.DEBUG,"Invalid value for UseCursorName parameter :"+tmpUCrN); 729 return DEFAULT_USE_CURSOR_NAME; 730 } 731 } 732 }else{ 733 return dbConf.getUseCursorName().booleanValue(); 734 } 735 } 736 737 738 742 public void setUseCursorName(boolean use) { 743 dbConf.setUseCursorName(use); 744 } 745 746 747 751 public void setUseCursorName(Boolean use) { 752 dbConf.setUseCursorName(use); 753 } 754 755 756 760 private void initResultSetType() { 761 String tmpUCrN = getDriverProperty(PARAMNAME_RESULT_SET_TYPE); 762 try { 763 if (tmpUCrN==null) 764 dbConf.setResultSetType(DEFAULT_RESULT_SET_TYPE); 765 if(tmpUCrN.equalsIgnoreCase("TYPE_SCROLL_SENSITIVE")) 766 dbConf.setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE); 767 else if (tmpUCrN.equalsIgnoreCase("TYPE_SCROLL_INSENSITIVE")) 768 dbConf.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE); 769 else if (tmpUCrN.equalsIgnoreCase("TYPE_FORWARD_ONLY")) 770 dbConf.setResultSetType(ResultSet.TYPE_FORWARD_ONLY); 771 else { 772 DODS.getLogChannel().write(Logger.DEBUG,"Invalid value for ResultSetType parameter :"+tmpUCrN); 773 dbConf.setResultSetType(DEFAULT_RESULT_SET_TYPE); 774 } 775 }catch(Exception ex) { 776 DODS.getLogChannel().write(Logger.DEBUG,"Use default value for ResultSetType parameter "); 777 } 778 } 779 780 781 785 786 public int getResultSetType(){ 787 return dbConf.getResultSetType(); 788 } 789 790 791 795 private void initResultSetConcurrency() { 796 String tmpUCrN = getDriverProperty(PARAMNAME_RESULT_SET_CONCURRENCY); 797 try{ 798 if (tmpUCrN==null) 799 dbConf.setResultSetConcurrency(DEFAULT_RESULT_SET_CONCURRENCY); 800 if(tmpUCrN.equalsIgnoreCase("CONCUR_READ_ONLY")) 801 dbConf.setResultSetConcurrency(ResultSet.CONCUR_READ_ONLY); 802 else if (tmpUCrN.equalsIgnoreCase("CONCUR_UPDATABLE")) 803 dbConf.setResultSetConcurrency(ResultSet.CONCUR_UPDATABLE); 804 else { 805 DODS.getLogChannel().write(Logger.DEBUG,"Invalid value for ResultSetConcurrency parameter :"+tmpUCrN); 806 dbConf.setResultSetConcurrency(DEFAULT_RESULT_SET_CONCURRENCY); 807 } 808 }catch(Exception ex) { 809 DODS.getLogChannel().write(Logger.DEBUG,"Use default value for ResultSetType parameter "); 810 } 811 } 812 813 817 public int getResultSetConcurrency(){ 818 return dbConf.getResultSetConcurrency(); 819 } 820 821 826 public DriverDependencies getDriverDependencies() { 827 if (null == drivDepsInstance) { 828 String className = getDriverProperty(PARAMNAME_DRIV_DEP_CLASS); 829 try { 830 if (null != className) { 831 drivDepsInstance = (DriverDependencies) 832 Class.forName(className) 833 .newInstance(); 834 } 835 } catch (Exception e) { 836 DODS.getLogChannel() 837 .write(Logger.ERROR, 838 "Didn't instantiated DriverDepenencies " 839 + className, 840 e); 841 } finally { 842 if (null == drivDepsInstance) 843 drivDepsInstance = new DriverDependencies() { 844 public boolean isBlobAccessSpecial() { return false;} 845 public byte[] readBlob(ResultSet rs, String c){ return null;} 846 public void insertBlob(DBConnection conn, 847 byte[] cont, 848 RDBColumn primary, 849 RDBColumn blobColumn, 850 String handle){} 851 }; 852 } 853 } 854 return drivDepsInstance; 855 } 856 } 857 | Popular Tags |