1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.impl; 31 32 import com.sun.jdo.api.persistence.support.*; 33 import com.sun.jdo.spi.persistence.utility.I18NHelper; 34 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceStore; 35 import com.sun.jdo.spi.persistence.support.sqlstore.SQLStoreManager; 36 import com.sun.jdo.spi.persistence.support.sqlstore.VersionConsistencyCache; 37 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 38 import com.sun.jdo.spi.persistence.utility.BucketizedHashtable; 39 import com.sun.jdo.spi.persistence.utility.logging.Logger; 40 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager; 41 42 44 import javax.sql.DataSource ; 45 import java.sql.SQLException ; 46 import java.io.PrintWriter ; 47 import java.sql.Connection ; 48 import java.util.Map ; 49 import java.util.Properties ; 50 import java.util.ResourceBundle ; 51 52 57 58 public class SQLPersistenceManagerFactory 59 implements com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManagerFactory { 60 61 private PersistenceStore _store = null; 62 private ConnectionFactory _connectionFactory = null; 63 private Object _dataSource = null; 64 private PersistenceManagerFactory _persistenceManagerFactory = null; 65 66 69 private boolean optimistic = true; 70 private boolean retainValues = true; 71 private boolean nontransactionalRead = true; 72 private boolean ignoreCache = true; 73 74 77 private int queryTimeout = 0; 78 private int updateTimeout = 0; 79 80 82 private int minPool = 0; 83 private int maxPool = 0; 84 85 88 private static Logger logger = LogHelperPersistenceManager.getLogger(); 89 90 93 private final static ResourceBundle messages = I18NHelper.loadBundle( 94 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", SQLPersistenceManagerFactory.class.getClassLoader()); 96 97 100 private static int pmCacheBucketSize; 101 102 105 private static int pmCacheInitialCapacity; 106 107 static { 108 pmCacheBucketSize = Integer.getInteger( 109 "com.sun.jdo.spi.persistence.support.sqlstore.impl.SQLPersistenceManagerFactory.pmCacheBucketSize", 11).intValue(); 111 112 pmCacheInitialCapacity = Integer.getInteger( 113 "com.sun.jdo.spi.persistence.support.sqlstore.impl.SQLPersistenceManagerFactory.pmCacheInitialCapacity", 11 * pmCacheBucketSize).intValue(); 115 116 if (logger.isLoggable(Logger.FINEST)) { 117 logger.finest( 118 "sqlstore.sqlpersistencemgrfactory.pmCacheBucketSize", String.valueOf(pmCacheBucketSize)); 120 logger.finest( 121 "sqlstore.sqlpersistencemgrfactory.pmCacheInitialCapacity", String.valueOf(pmCacheInitialCapacity)); 123 } 124 } 125 126 129 private Map pmCache = new BucketizedHashtable(pmCacheBucketSize, 130 pmCacheInitialCapacity); 131 132 135 private VersionConsistencyCache vcCache = null; 136 137 140 public SQLPersistenceManagerFactory() { 141 142 } 143 144 148 public SQLPersistenceManagerFactory(Object connectionFactory) { 149 if (connectionFactory instanceof ConnectionFactory) 150 _connectionFactory = (ConnectionFactory) connectionFactory; 151 else 152 _dataSource = connectionFactory; 153 154 if (this instanceof PersistenceManagerFactory) 155 _persistenceManagerFactory = this; 156 157 initialize(); 158 } 159 160 164 public SQLPersistenceManagerFactory( 165 PersistenceManagerFactory persistenceManagerFactory) { 166 _persistenceManagerFactory = persistenceManagerFactory; 167 Object cf = _persistenceManagerFactory.getConnectionFactory(); 168 169 if (cf instanceof ConnectionFactory) 170 _connectionFactory = (ConnectionFactory) cf; 171 else 172 _dataSource = cf; 173 174 initialize(); 175 } 176 177 181 public void setConnectionUserName(String userName) { 182 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 183 "jdo.persistencemanagerfactoryimpl.notsupported")); } 185 186 190 public String getConnectionUserName() { 191 return _persistenceManagerFactory.getConnectionUserName(); 192 } 193 194 198 public void setConnectionPassword(String password) { 199 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 200 "jdo.persistencemanagerfactoryimpl.notsupported")); } 202 203 207 public void setConnectionURL(String url) { 208 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 209 "jdo.persistencemanagerfactoryimpl.notsupported")); } 211 212 216 public String getConnectionURL() { 217 return _persistenceManagerFactory.getConnectionURL(); 218 219 } 220 221 225 public void setConnectionDriverName(String driverName) { 226 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 227 "jdo.persistencemanagerfactoryimpl.notsupported")); } 229 230 234 public String getConnectionDriverName() { 235 return _persistenceManagerFactory.getConnectionDriverName(); 236 237 } 238 239 243 public void setConnectionFactory(Object cf) { 244 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 245 "jdo.persistencemanagerfactoryimpl.notsupported")); } 247 248 252 public Object getConnectionFactory() { 253 if (_dataSource != null) 254 return _dataSource; 255 256 return _connectionFactory; 257 } 258 259 263 public void setOptimistic(boolean flag) { 264 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 265 "jdo.persistencemanagerfactoryimpl.notsupported")); } 267 268 272 public boolean getOptimistic() { 273 return _persistenceManagerFactory.getOptimistic(); 274 } 275 276 280 public void setRetainValues(boolean flag) { 281 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 282 "jdo.persistencemanagerfactoryimpl.notsupported")); } 284 285 290 public boolean getRetainValues() { 291 return _persistenceManagerFactory.getRetainValues(); 292 } 293 294 298 public void setNontransactionalRead(boolean flag) { 299 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 300 "jdo.persistencemanagerfactoryimpl.notsupported")); } 302 303 308 public boolean getNontransactionalRead() { 309 return _persistenceManagerFactory.getNontransactionalRead(); 310 } 311 312 313 318 public void setIgnoreCache(boolean flag) { 319 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 320 "jdo.persistencemanagerfactoryimpl.notsupported")); } 322 323 328 public boolean getIgnoreCache() { 329 return _persistenceManagerFactory.getIgnoreCache(); 330 } 331 332 337 public void setQueryTimeout(int timeout) { 338 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 339 "jdo.persistencemanagerfactoryimpl.notsupported")); } 341 342 347 public int getQueryTimeout() { 348 return _persistenceManagerFactory.getQueryTimeout(); 349 } 350 351 355 public void setConnectionMaxPool(int MaxPool) { 356 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 357 "jdo.persistencemanagerfactoryimpl.notsupported")); } 359 360 364 public int getConnectionMaxPool() { 365 return _persistenceManagerFactory.getConnectionMaxPool(); 366 } 367 368 372 public void setConnectionMinPool(int MinPool) { 373 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 374 "jdo.persistencemanagerfactoryimpl.notsupported")); } 376 377 381 public int getConnectionMinPool() { 382 return _persistenceManagerFactory.getConnectionMinPool(); 383 } 384 385 390 public void setConnectionMsWait(int MsWait) { 391 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 392 "jdo.persistencemanagerfactoryimpl.notsupported")); } 394 395 400 public int getConnectionMsWait() { 401 return _persistenceManagerFactory.getConnectionMsWait(); 402 } 403 404 411 public void setConnectionMsInterval(int MsInterval) { 412 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 413 "jdo.persistencemanagerfactoryimpl.notsupported")); } 415 416 421 public int getConnectionMsInterval() { 422 return _persistenceManagerFactory.getConnectionMsInterval(); 423 } 424 425 430 public void setConnectionLoginTimeout(int LoginTimeout) { 431 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 432 "jdo.persistencemanagerfactoryimpl.notsupported")); } 434 435 440 public int getConnectionLoginTimeout() { 441 return _persistenceManagerFactory.getConnectionLoginTimeout(); 442 } 443 444 448 public void setConnectionLogWriter(PrintWriter pw) { 449 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 450 "jdo.persistencemanagerfactoryimpl.notsupported")); } 452 453 457 public PrintWriter getConnectionLogWriter() { 458 return _persistenceManagerFactory.getConnectionLogWriter(); 459 } 460 461 469 public void setConnectionTransactionIsolation(int level) { 470 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 471 "jdo.persistencemanagerfactoryimpl.notsupported")); } 473 474 478 public int getConnectionTransactionIsolation() { 479 return _persistenceManagerFactory.getConnectionTransactionIsolation(); 480 } 481 482 486 public void setConnectionFactoryName(String connectionFactoryName) { 487 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 488 "jdo.persistencemanagerfactoryimpl.notsupported")); } 490 491 495 public String getConnectionFactoryName() { 496 return _persistenceManagerFactory.getConnectionFactoryName(); 497 } 498 499 503 public void setIdentifier(String identifier) { 504 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 505 "jdo.persistencemanagerfactoryimpl.notsupported")); } 507 508 512 public String getIdentifier() { 513 return _persistenceManagerFactory.getIdentifier(); 514 } 515 516 520 public int getMaxPool() { 521 return maxPool; 522 } 523 524 525 529 public void setMaxPool(int MaxPool) { 530 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 531 "jdo.persistencemanagerfactoryimpl.notsupported")); } 533 534 538 public int getMinPool() { 539 return minPool; 540 } 541 542 543 547 public void setMinPool(int MinPool) { 548 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 549 "jdo.persistencemanagerfactoryimpl.notsupported")); } 551 552 553 558 public void setUpdateTimeout(int timeout) { 559 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 560 "jdo.persistencemanagerfactoryimpl.notsupported")); } 562 563 568 public int getUpdateTimeout() { 569 return _persistenceManagerFactory.getUpdateTimeout(); 570 } 571 572 579 public boolean getSupersedeDeletedInstance () { 580 return _persistenceManagerFactory.getSupersedeDeletedInstance(); 581 } 582 583 584 588 public void setSupersedeDeletedInstance (boolean flag) { 589 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 590 "jdo.persistencemanagerfactoryimpl.notsupported")); } 592 593 602 public boolean getRequireCopyObjectId() { 603 return _persistenceManagerFactory.getRequireCopyObjectId(); 604 } 605 606 607 617 public void setRequireCopyObjectId (boolean flag) { 618 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 619 "jdo.persistencemanagerfactoryimpl.notsupported")); } 621 622 631 public boolean getRequireTrackedSCO() { 632 return _persistenceManagerFactory.getRequireTrackedSCO(); 633 } 634 635 643 public void setRequireTrackedSCO (boolean flag) { 644 throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages, 645 "jdo.persistencemanagerfactoryimpl.notsupported")); } 647 648 654 public PersistenceManager getPersistenceManager() { 655 return getPersistenceManager(null, null); 656 } 657 658 666 public PersistenceManager getPersistenceManager(String username, String password) { 667 boolean debug = logger.isLoggable(Logger.FINEST); 668 669 if (_connectionFactory == null && _dataSource == null) { 670 throw new JDOUserException(I18NHelper.getMessage(messages, 671 "jdo.persistencemanagerfactoryimpl.getpersistencemanager.notconfigured")); } 673 674 if (debug) { 675 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr",Thread.currentThread()); } 677 678 PersistenceManagerImpl pm = null; 680 javax.transaction.Transaction t = EJBHelper.getTransaction(); 681 682 if (t != null) { 683 if (debug) { 684 Object [] items = new Object [] {Thread.currentThread(),t}; 685 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.found",items); } 687 688 pm = (PersistenceManagerImpl) pmCache.get(t); 689 if (pm == null) { 690 pm = getFromPool(t, username, password); 692 pmCache.put(t, pm); 693 } else if(pm.isClosed()) { 694 if (debug) { 695 Object [] items = new Object [] {Thread.currentThread(),t}; 696 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.pmclosedfor",items); } 698 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 699 "jdo.persistencemanagerfactoryimpl.getpersistencemanager.closed", t)); 701 } 702 703 707 com.sun.jdo.spi.persistence.support.sqlstore.Transaction tx = 708 (com.sun.jdo.spi.persistence.support.sqlstore.Transaction) pm.currentTransaction(); 709 if (debug) { 710 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.jdotx",tx); } 712 713 if (!tx.isActive()) { 714 tx.begin(t); 715 } 716 717 if (!(pm.verify(username, password))) { 718 ; 719 throw new JDOUserException(I18NHelper.getMessage(messages, 720 "jdo.persistencemanagerfactoryimpl.getpersistencemanager.error")); } 722 } else { 723 if (debug) { 724 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.jdotx.notfound"); } 726 pm = getFromPool(null, username, password); 730 } 731 732 if (debug) { 733 Object [] items = new Object [] {Thread.currentThread(),pm,t}; 734 logger.finest("sqlstore.sqlpersistencemgrfactory.getpersistencemgr.pmt",items); } 736 737 return new PersistenceManagerWrapper(pm); 739 740 } 741 742 746 public Properties getProperties() { 747 return _persistenceManagerFactory.getProperties(); 748 } 749 750 753 public PersistenceManagerFactory getPersistenceManagerFactory() { 754 return _persistenceManagerFactory; 755 } 756 757 763 public void registerPersistenceManager( 764 com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager pm, 765 javax.transaction.Transaction t) { 766 767 boolean debug = logger.isLoggable(Logger.FINEST); 768 if (debug) { 769 Object [] items = new Object [] {pm,t}; 770 logger.finest("sqlstore.sqlpersistencemgrfactory.registerpersistencemgr.pmt",items); } 772 PersistenceManager pm1 = (PersistenceManager) pmCache.get(t); 773 if (pm1 == null) { 775 pmCache.put(t, pm); 776 ((PersistenceManagerImpl) pm).setJTATransaction(t); 777 return; 778 } 779 780 if (pm1 != pm) { 781 Object [] items = new Object [] {t, pm1}; 782 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 783 "jdo.persistencemanagerfactoryimpl.registerpm.registered", items)); 785 } else { 786 } 788 } 789 790 794 private PersistenceManagerImpl getFromPool(javax.transaction.Transaction tx, 795 String username, String password) { 796 797 boolean debug = logger.isLoggable(Logger.FINEST); 798 if (debug) { 799 logger.finest("sqlstore.sqlpersistencemgrfactory.getfrompool"); } 801 802 synchronized (this) { 803 if (_store == null) { 804 initializeSQLStoreManager(username, password); 805 } 806 } 807 808 PersistenceManagerImpl pm = new PersistenceManagerImpl(this, tx, username, password); 810 pm.setStore(_store); 811 if (debug) { 812 Object [] items = new Object [] {pm,tx}; 813 logger.finest("sqlstore.sqlpersistencemgrfactory.getfrompool.pmt",items); } 815 816 return pm; 817 818 } 819 820 823 private void returnToPool(PersistenceManager pm) { 824 logger.finest("sqlstore.sqlpersistencemgrfactory.returnToPool"); } 827 828 830 public void releasePersistenceManager(com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager pm, 831 javax.transaction.Transaction t) { 832 833 834 boolean debug = logger.isLoggable(Logger.FINEST); 835 if (debug) { 836 Object [] items = new Object [] {pm,t}; 837 logger.finest("sqlstore.sqlpersistencemgrfactory.releasepm.pmt",items); 839 } 840 841 if (t != null) { 842 PersistenceManager pm1 = (PersistenceManager) pmCache.get(t); 845 if (pm1 == null || pm1 != pm) { 846 Object [] items = new Object [] {t, pm1}; 847 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 848 "jdo.persistencemanagerfactoryimpl.registerpm.registered", items)); 850 } else { 851 pmCache.remove(t); 852 } 853 } else { 854 returnToPool(pm); 855 } 856 } 857 858 private void initialize() { 859 860 logger.finest("sqlstore.sqlpersistencemgrfactory.init"); optimistic = _persistenceManagerFactory.getOptimistic(); 862 retainValues = _persistenceManagerFactory.getRetainValues(); 863 nontransactionalRead = _persistenceManagerFactory.getNontransactionalRead(); 864 ignoreCache = _persistenceManagerFactory.getIgnoreCache(); 865 queryTimeout = _persistenceManagerFactory.getQueryTimeout(); 866 updateTimeout = _persistenceManagerFactory.getUpdateTimeout(); 867 minPool = _persistenceManagerFactory.getMinPool(); 868 maxPool = _persistenceManagerFactory.getMaxPool(); 869 } 870 871 872 private void initializeSQLStoreManager(String username, String password) { 873 Connection conn = null; 874 try { 875 conn = getConnection(username, password); 876 if (conn != null) { 877 _store = new SQLStoreManager(conn.getMetaData(), 878 getIdentifier() ); 879 } 880 } catch(Exception e) { 881 if (logger.isLoggable(Logger.WARNING)) { 882 logger.log(Logger.WARNING, "jdo.sqlpersistencemanagerfactory.errorgettingDatabaseInfo", e); } 884 885 if (e instanceof JDOException) { 886 throw (JDOException) e; 887 } else { 888 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 889 "core.configuration.getvendortypefailed"), e); } 891 } finally { 892 if (conn != null) { 893 try { 894 conn.close(); 895 } catch(Exception ex) {} 896 } 897 } 898 899 } 900 901 904 private Connection getConnection(String username, String password) throws SQLException { 905 Connection conn = null; 906 907 if (_connectionFactory != null) { 908 conn = _connectionFactory.getConnection(); 909 } else if (EJBHelper.isManaged()) { 910 conn = EJBHelper.getConnection(_dataSource, username, password); 911 } else { 912 if (username == null) { 913 conn = ((DataSource )_dataSource).getConnection(); 914 } else { 915 conn = ((DataSource )_dataSource).getConnection(username, password); 916 } 917 } 918 919 return conn; 920 } 921 922 928 public boolean equals(Object obj) { 929 if ((obj != null) && (obj instanceof SQLPersistenceManagerFactory)) { 930 SQLPersistenceManagerFactory pmf = (SQLPersistenceManagerFactory) obj; 931 return (pmf._persistenceManagerFactory.equals(this._persistenceManagerFactory)); 932 933 } 934 return false; 935 } 936 937 942 public int hashCode() { 943 return this._persistenceManagerFactory.hashCode(); 944 } 945 946 951 public VersionConsistencyCache getVersionConsistencyCache() { 952 if (null == vcCache) { 953 if (_store == null) { 954 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 956 "jdo.persistencemanagerfactoryimpl.getversionconsistencycache.nullstore")); } 958 vcCache = VersionConsistencyCacheImpl.create(); 959 _store.getConfigCache().setVersionConsistencyCache(vcCache); 960 } 961 return vcCache; 962 } 963 } 964 | Popular Tags |