1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.impl; 31 32 import java.lang.String ; 33 import java.io.PrintWriter ; 34 import java.sql.Connection ; 35 import java.sql.DatabaseMetaData ; 36 import java.sql.SQLException ; 37 import java.util.ResourceBundle ; 38 39 40 import com.sun.jdo.api.persistence.support.*; 41 import com.sun.jdo.spi.persistence.support.sqlstore.connection.ConnectionManager; 42 import com.sun.jdo.spi.persistence.utility.I18NHelper; 43 44 49 public class ConnectionFactoryImpl implements ConnectionFactory, java.io.Serializable { 50 51 53 private String URL = null; 54 private String userName = null; 55 private String password = null; 56 private String driverName = null; 57 private int maxPool = 0; 58 private int minPool = 0; 59 private int msInterval = 0; 60 private int loginTimeout = 0; 61 private int msWait = 0; 62 private int _txIsolation = -1; 63 private PrintWriter logWriter = null; 64 65 private transient boolean _configured = false; 66 private transient ConnectionManager connectionManager = null; 67 68 71 private transient final static ResourceBundle messages = I18NHelper.loadBundle( 72 ConnectionFactoryImpl.class); 73 74 75 76 79 public ConnectionFactoryImpl() 80 { 81 } 83 84 91 public ConnectionFactoryImpl( 92 String URL, 93 String userName, 94 String password, 95 String driverName 96 ) 97 { 98 this.driverName = driverName; 99 this.URL = URL; 100 this.userName = userName; 101 this.password = password; 102 103 } 104 105 114 public ConnectionFactoryImpl( 115 String URL, 116 String userName, 117 String password, 118 String driverName, 119 int minPool, 120 int maxPool 121 ) 122 { 123 this.driverName = driverName; 124 this.URL = URL; 125 this.userName = userName; 126 this.password = password; 127 this.minPool = minPool; 128 this.maxPool = maxPool; 129 130 } 131 132 136 public void setDriverName (String driverName) 137 { 138 141 assertNotConfigured(); 142 if(connectionManager == null) 144 { 145 this.driverName = driverName; 146 } 147 else 148 { 149 try { 150 connectionManager.setDriverName(driverName); 151 } catch (Exception e) { 152 throw new JDOFatalException(null, e); 153 } 154 } 155 } 156 157 161 public String getDriverName () 162 { 163 return driverName; 164 } 166 167 168 172 public void setURL (String URL) 173 { 174 177 assertNotConfigured(); 178 if(connectionManager == null) 180 { 181 this.URL = URL; 182 } 183 else 184 { 185 try { 186 connectionManager.setURL(URL); 187 } catch (Exception e) { 188 throw new JDOFatalException(null, e); 189 } 190 } 191 } 192 193 197 public String getURL () 198 { 199 return URL; 200 } 202 203 204 208 public void setUserName (String userName) 209 { 210 213 assertNotConfigured(); 214 if(connectionManager == null) 216 { 217 this.userName = userName; 218 } 219 else 220 { 221 try { 222 connectionManager.setUserName(userName); 223 } catch (Exception e) { 224 throw new JDOFatalException(null, e); 225 } 226 } 227 } 228 229 233 public String getUserName () 234 { 235 return userName; 236 } 238 239 240 244 public void setPassword (String password) 245 { 246 249 assertNotConfigured(); 250 if(connectionManager == null) 252 { 253 this.password = password; 254 } 255 else 256 { 257 try { 258 connectionManager.setPassword(password); 259 } catch (Exception e) { 260 throw new JDOFatalException(null, e); 261 } 262 } 263 } 264 265 266 270 public void setMinPool (int minPool) 271 { 272 assertNotConfigured(); 273 if(connectionManager == null) { 274 this.minPool = minPool; 276 return; 277 } 278 279 try { 281 connectionManager.setMinPool(minPool); 282 } catch (Exception e) { 283 throw new JDOFatalException(null, e); 284 } 285 } 286 287 291 public int getMinPool () 292 { 293 return minPool; 294 } 296 297 298 302 public void setMaxPool (int maxPool) 303 { 304 assertNotConfigured(); 305 if(connectionManager == null) { 306 this.maxPool = maxPool; 308 return; 309 } 310 311 try { 313 connectionManager.setMaxPool(maxPool); 314 } catch (Exception e) { 315 throw new JDOFatalException(null, e); 316 } 317 } 318 319 323 public int getMaxPool () 324 { 325 return maxPool; 326 } 328 329 330 336 public void setMsInterval (int msInterval) 337 { 338 assertNotConfigured(); 339 if(connectionManager == null) { 340 this.msInterval = msInterval; 342 return; 343 } 344 345 try { 347 connectionManager.setMsInterval(msInterval); 348 } catch (Exception e) { 349 throw new JDOFatalException(null, e); 350 } 351 } 352 353 358 public int getMsInterval () 359 { 360 if (connectionManager == null) 361 return msInterval; 362 363 return connectionManager.getMsInterval(); 364 } 365 366 367 372 public void setMsWait (int msWait) 373 { 374 assertNotConfigured(); 375 if(connectionManager == null) { 376 this.msWait = msWait; 378 return; 379 } 380 381 try { 383 connectionManager.setMsWait(msWait); 384 } catch (Exception e) { 385 throw new JDOFatalException(null, e); 386 } 387 } 388 389 394 public int getMsWait () 395 { 396 if (connectionManager == null) 397 return msWait; 398 399 return connectionManager.getMsWait(); 400 } 401 402 403 407 public void setLogWriter (PrintWriter logWriter) 408 { 409 assertNotConfigured(); 410 this.logWriter = logWriter; } 412 413 417 public PrintWriter getLogWriter () 418 { 419 return logWriter; 420 } 421 422 423 428 public void setLoginTimeout (int loginTimeout) 429 { 430 assertNotConfigured(); 431 if(connectionManager == null) { 432 this.loginTimeout = loginTimeout; 434 return; 435 } 436 437 try { 439 connectionManager.setLoginTimeout(loginTimeout); 440 } catch (Exception e) { 441 throw new JDOFatalException(null, e); 442 } 443 } 444 445 450 public int getLoginTimeout () 451 { 452 if (connectionManager == null) 453 return loginTimeout; 454 455 try { 456 return connectionManager.getLoginTimeout(); 457 } catch (Exception e) { 458 return 0; 459 } 460 } 461 470 public void setTransactionIsolation (int level) 471 { 472 assertNotConfigured(); 473 if(connectionManager == null) { 474 _txIsolation = level; 476 return; 477 } 478 479 Connection conn = null; 481 try { 482 conn = connectionManager.getConnection(); 483 DatabaseMetaData dbMetaData = conn.getMetaData(); 484 if(dbMetaData.supportsTransactionIsolationLevel(level)) 485 { 486 _txIsolation = level; 487 } 488 else 489 { 490 throw new JDOFatalException(I18NHelper.getMessage(messages, 491 "connectionefactoryimpl.isolationlevel_notsupported", level)); 493 } 494 } catch (Exception e) { 495 throw new JDOFatalException(null, e); 496 } 497 finally 498 { 499 closeConnection(conn); 500 } 501 } 502 503 507 public int getTransactionIsolation () 508 { 509 if (connectionManager == null) 510 return _txIsolation; 511 512 Connection conn = null; 513 try { 514 if (_txIsolation == -1) 516 { 517 synchronized(this) 518 { 519 if (_txIsolation == -1) 521 { 522 conn = connectionManager.getConnection(); 523 _txIsolation = conn.getTransactionIsolation(); 524 } 525 } 526 } 527 528 return _txIsolation; 529 } catch (Exception e) { 530 throw new JDOFatalException(null, e); 531 } 532 finally 533 { 534 closeConnection(conn); 535 } 536 } 537 538 539 543 public Connection getConnection() 544 { 545 try { 547 if (connectionManager == null) 548 initialize(); 549 550 Connection conn = connectionManager.getConnection(); 551 conn.setTransactionIsolation(_txIsolation); 552 553 return conn; 554 555 } catch (SQLException e) { 556 String sqlState = e.getSQLState(); 557 int errorCode = e.getErrorCode(); 558 559 if (sqlState == null) 560 { 561 throw new JDODataStoreException(I18NHelper.getMessage(messages, 562 "connectionefactoryimpl.sqlexception", "null", "" + errorCode), e); } 564 else 565 { 566 throw new JDODataStoreException(I18NHelper.getMessage(messages, 567 "connectionefactoryimpl.sqlexception", sqlState, "" + errorCode), e); } 569 } catch (Exception e) { 570 throw new JDOCanRetryException(I18NHelper.getMessage(messages, 571 "connectionefactoryimpl.getconnection"), e); } 573 } 574 575 581 public boolean equals(Object obj) { 582 if ((obj != null) && (obj instanceof ConnectionFactoryImpl) ) { 583 ConnectionFactoryImpl cf = (ConnectionFactoryImpl)obj; 584 return (cf.URL.equals(this.URL) && cf.userName.equals(this.userName) && cf.driverName.equals(this.driverName) && cf.password.equals(this.password)); 585 } 586 return false; 587 } 588 589 594 public int hashCode() { 595 return URL.hashCode() + userName.hashCode() + password.hashCode() + driverName.hashCode(); 596 } 597 598 599 604 public void configured(boolean flag) 605 { 606 _configured = flag; 607 } 608 609 613 private void assertNotConfigured() { 614 if ( _configured) { 615 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 616 "persistencemanagerfactoryimpl.configured")); } 618 } 619 620 624 private void assertConnectionWait() { 625 if ( msWait < 0 ) 626 { 627 throw new JDOUserException(I18NHelper.getMessage(messages, 628 "connection.connectionmanager.mswaitvalue")); } 630 else if ( msInterval < 0 || msInterval > msWait || (msWait > 0 && msInterval == 0) ) 631 { 632 throw new JDOUserException(I18NHelper.getMessage(messages, 633 "connection.connectionmanager.msintervalvalue")); } 635 } 636 637 638 643 private synchronized void initialize() { 644 if (connectionManager != null) 646 return; 647 648 try { 649 assertConnectionWait(); 651 652 connectionManager = new ConnectionManager( 656 driverName, 657 URL, 658 userName, 659 password, 660 minPool, 661 maxPool 662 ); 663 connectionManager.setMsWait(this.msWait); 665 connectionManager.setMsInterval(this.msInterval); 666 connectionManager.setLoginTimeout(this.loginTimeout); 667 668 if (_txIsolation > 0) 669 setTransactionIsolation(_txIsolation); 670 else 671 _txIsolation = getTransactionIsolation(); 672 673 this.configured(true); 675 676 } catch (JDOException e) { 677 throw e; 678 } catch (Exception e) { 679 throw new JDOFatalException(null, e); 680 } 681 } 682 683 687 private void unsupported() { 688 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 689 "persistencemanagerfactoryimpl.notsupported")); } 691 692 695 private void closeConnection(Connection conn) 696 { 697 try 698 { 699 if (conn != null) conn.close(); 700 } 701 catch (Exception e) 702 { 703 } 705 706 conn = null; 707 708 } 709 } 710 | Popular Tags |