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.api.persistence.support.Transaction; 34 import com.sun.jdo.spi.persistence.support.sqlstore.*; 35 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable; 36 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager; 37 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManagerFactory; 38 import com.sun.jdo.spi.persistence.support.sqlstore.ejb.EJBHelper; 39 import com.sun.jdo.spi.persistence.support.sqlstore.query.QueryImpl; 40 import com.sun.jdo.spi.persistence.utility.I18NHelper; 41 import com.sun.jdo.spi.persistence.utility.NullSemaphore; 42 import com.sun.jdo.spi.persistence.utility.Semaphore; 43 import com.sun.jdo.spi.persistence.utility.SemaphoreImpl; 44 import com.sun.jdo.spi.persistence.utility.logging.Logger; 45 46 import javax.transaction.Status ; 47 import java.io.File ; 48 import java.io.FilenameFilter ; 49 import java.lang.reflect.Constructor ; 50 import java.lang.reflect.Field ; 51 import java.util.*; 52 53 public class PersistenceManagerImpl implements PersistenceManager { 54 57 private boolean _isClosed = true; 58 59 private PersistenceManagerWrapper current = null; 61 62 private javax.transaction.Transaction _jta = null; 64 65 68 private PersistenceStore _store = null; 69 70 73 private TransactionImpl _transaction = null; 74 75 79 private PersistenceManagerFactory persistenceManagerFactory = null; 80 81 82 86 private List _txCache; 87 88 92 private Set _flushedCache; 93 94 100 private Map _weakCache; 101 102 108 private boolean cleanupVersionConsistencyCache = false; 109 110 116 121 private static final int _txCacheInitialCapacity = Integer.getInteger( 122 "com.sun.jdo.api.persistence.support.PersistenceManager.dirtyCache.initialCapacity", 20).intValue(); 124 125 130 private static final int _flushedCacheInitialCapacity = Integer.getInteger( 131 "com.sun.jdo.api.persistence.support.PersistenceManager.transactionalCache.initialCapacity", 20).intValue(); 133 134 139 private static final float _flushedCacheLoadFactor; 140 141 static { 142 float f = (float) 0.75; 143 try { 144 f = 145 Float.valueOf( 146 System.getProperty( 147 "com.sun.jdo.api.persistence.support.PersistenceManager.transactionalCache.loadFactor", "0.75")).floatValue(); } finally { 150 _flushedCacheLoadFactor = f; 151 } 152 } 153 154 159 private static final int _weakCacheInitialCapacity = Integer.getInteger( 160 "com.sun.jdo.api.persistence.support.PersistenceManager.globalCache.initialCapacity", 20).intValue(); 162 163 168 private static final float _weakCacheLoadFactor; 169 170 static { 171 float f = (float) 0.75; 172 try { 173 f = 174 Float.valueOf( 175 System.getProperty( 176 "com.sun.jdo.api.persistence.support.PersistenceManager.globalCache.loadFactor", "0.75")).floatValue(); } finally { 179 _weakCacheLoadFactor = f; 180 } 181 } 182 183 184 private Collection queries = new ArrayList(); 185 186 189 private boolean _ignoreCache = true; 190 191 194 private boolean _optimistic = true; 195 196 199 private boolean _supersedeDeletedInstance = true; 200 201 204 private boolean _requireCopyObjectId = true; 205 206 209 private boolean _requireTrackedSCO = true; 210 211 214 private boolean _nontransactionalRead = true; 215 216 219 private boolean _activeTransaction = false; 220 221 224 private Object _userObject = null; 225 226 229 private boolean _insideCommit = false; 230 231 234 private boolean _insideFlush = false; 235 236 239 private static final String oidName_OID = "OID"; private static final String oidName_KEY = "KEY"; 242 245 private Properties _properties = null; 246 247 250 private static final Class [] sigSM = new Class []{ 251 StateManager.class}; 252 253 256 private final Semaphore _cacheLock; 257 258 262 private final Semaphore _fieldUpdateLock; 263 264 267 private Object _readWriteLock = new Object (); 268 269 274 private long _readWriteCount = 0; 275 276 280 private long _waiterCount = 0; 281 282 285 private Thread _exclusiveLockHolder = null; 286 287 290 private final boolean _multithreaded; 291 292 295 private static Logger logger = LogHelperPersistenceManager.getLogger(); 296 297 300 private final static ResourceBundle messages = I18NHelper.loadBundle( 301 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", PersistenceManagerImpl.class.getClassLoader()); 303 304 307 PersistenceManagerImpl(PersistenceManagerFactory pmf, javax.transaction.Transaction t, 308 String username, String password) { 309 persistenceManagerFactory = pmf; 310 311 if (logger.isLoggable(Logger.FINEST)) { 313 Object [] items = new Object [] { new Integer (_txCacheInitialCapacity), 314 new Integer (_flushedCacheInitialCapacity), 315 new Float (_flushedCacheLoadFactor), 316 new Integer (_weakCacheInitialCapacity), 317 new Float (_weakCacheLoadFactor) }; 318 logger.finest("sqlstore.persistencemgr.cacheproperties", items); } 320 _txCache = new ArrayList(_txCacheInitialCapacity); 321 _flushedCache = new LinkedHashSet(_flushedCacheInitialCapacity, _flushedCacheLoadFactor); 322 _weakCache = new HashMap(_weakCacheInitialCapacity, _weakCacheLoadFactor); 323 324 326 _transaction = new TransactionImpl(this, 327 username, password, TransactionImpl.TRAN_DEFAULT_TIMEOUT); 329 _ignoreCache = pmf.getIgnoreCache(); 330 _optimistic = pmf.getOptimistic(); 331 _nontransactionalRead = pmf.getNontransactionalRead(); 332 _supersedeDeletedInstance = pmf.getSupersedeDeletedInstance(); 333 _requireCopyObjectId = pmf.getRequireCopyObjectId(); 334 _requireTrackedSCO = pmf.getRequireTrackedSCO(); 335 336 this._jta = t; _isClosed = false; 338 339 _multithreaded = ( ! EJBHelper.isManaged()); 340 341 if (_multithreaded) { 342 _cacheLock = 343 new SemaphoreImpl("PersistenceManagerImpl.cacheLock"); _fieldUpdateLock = 345 new SemaphoreImpl("PersistenceManagerImpl.fieldUpdateLock"); } else { 347 if (_jta == null) { 348 _cacheLock = 351 new SemaphoreImpl("PersistenceManagerImpl.cacheLock"); } else { 353 _cacheLock = 354 new NullSemaphore("PersistenceManagerImpl.cacheLock"); } 356 _fieldUpdateLock = 357 new NullSemaphore("PersistenceManagerImpl.fieldUpdateLock"); } 359 } 360 361 364 protected void setStore(PersistenceStore store) { 365 _store = store; 366 } 367 368 371 protected PersistenceStore getStore() { 372 return _store; 373 } 374 375 378 protected boolean getIgnoreCache() { 379 return _ignoreCache; 380 } 381 382 385 protected boolean verify(String username, String password) { 386 return _transaction.verify(username, password); 387 } 388 389 392 public boolean isClosed() { 393 return _isClosed; 394 } 395 396 401 public void forceClose() { 402 403 406 persistenceManagerFactory.releasePersistenceManager(this, _jta); 407 408 _isClosed = true; 411 while (current != null) { 412 current.close(); 413 } 414 415 Collection c = _weakCache.values(); 416 for (Iterator it = c.iterator(); it.hasNext();) { 417 StateManager sm = (StateManager)it.next(); 418 419 422 425 sm.release(); } 428 429 disconnectQueries(); 430 431 persistenceManagerFactory = null; 432 _jta = null; 433 434 _weakCache.clear(); 435 _txCache.clear(); 436 _flushedCache.clear(); 437 438 _flushedCache = null; 439 _txCache = null; 440 _weakCache = null; 441 442 _store = null; 443 _transaction = null; 444 445 _exclusiveLockHolder = null; 446 } 447 448 451 public void close() { 452 453 acquireExclusiveLock(); 454 455 try { 456 if (_jta != null) { 457 persistenceManagerFactory.releasePersistenceManager(this, _jta); 459 _jta = null; 460 } 461 462 if (current != null && _transaction.getTransactionType() != TransactionImpl.CMT) { 463 469 return; } 471 472 if (_activeTransaction || _flushedCache.size() > 0) { 473 throw new JDOException(I18NHelper.getMessage(messages, 474 "jdo.persistencemanagerimpl.close.activetransaction")); } 476 477 forceClose(); 478 479 } finally { 480 releaseExclusiveLock(); 481 } 482 } 483 484 485 489 public Transaction currentTransaction() { 490 assertIsOpen(); 491 return _transaction; 492 } 493 494 495 498 public Query newQuery() { 499 assertIsOpen(); 500 QueryImpl q = new QueryImpl(this); 501 registerQuery(q); 502 return q; 503 } 504 505 512 public Query newQuery(Object compiled) { 513 assertIsOpen(); 514 QueryImpl q = new QueryImpl(this, compiled); 515 registerQuery(q); 516 return q; 517 } 518 519 523 public Query newQuery(Class cls) { 524 assertIsOpen(); 525 QueryImpl q = new QueryImpl(this, cls); 526 registerQuery(q); 527 return q; 528 } 529 530 536 public Query newQuery(Class cls, Collection cln) { 537 assertIsOpen(); 538 QueryImpl q = new QueryImpl(this, cls, cln); 539 registerQuery(q); 540 return q; 541 } 542 543 549 public Query newQuery(Class cls, String filter) { 550 assertIsOpen(); 551 QueryImpl q = new QueryImpl(this, cls, filter); 552 registerQuery(q); 553 return q; 554 } 555 556 563 public Query newQuery(Class cls, Collection cln, String filter) { 564 assertIsOpen(); 565 QueryImpl q = new QueryImpl(this, cls, cln, filter); 566 registerQuery(q); 567 return q; 568 } 569 570 579 public Collection getExtent(Class persistenceCapableClass, 580 boolean subclasses) { 581 assertIsOpen(); 582 return new ExtentCollection(this, persistenceCapableClass, subclasses); 583 } 584 585 598 public Object getObjectById(Object oid) { 599 return getObjectById(oid, false); 600 } 601 602 623 public Object getObjectById(Object oid, boolean validate) { 624 boolean debug = logger.isLoggable(Logger.FINEST); 625 626 assertIsOpen(); 627 assertActiveTransaction(true); 628 629 Object rc = null; 630 631 if (debug) { 632 Object [] items = new Object [] {oid, this,_jta}; 633 logger.finest("sqlstore.persistencemgr.getbyobjid", items); } 635 636 if (oid == null) 637 return null; 638 639 StateManager sm = lookupObjectById(oid, null); 640 rc = sm.getPersistent(); 641 if (!JDOHelper.isTransactional(rc)) { 642 643 boolean foundInstance = initializeFromVersionConsistencyCache(sm); 646 647 if (validate && !foundInstance) { 648 649 try { 651 sm.reload(); 652 653 } catch (JDOException e) { 654 if (!sm.isValid()) { 655 deregisterInstance(oid); 657 sm.release(); 658 } 659 660 throw e; 661 } catch (Exception e) { 662 throw new JDOUserException(I18NHelper.getMessage(messages, 663 "jdo.persistencemanagerimpl.fetchinstance.none"), e); } 665 } 666 } 667 668 sm.setValid(); 669 return rc; 670 } 671 672 680 public StateManager findOrCreateStateManager(Object oid, Class pcClass) { 681 return lookupObjectById(oid, pcClass); 682 } 683 684 689 private StateManager lookupObjectById(Object oid, Class classType) { 690 StateManager sm = null; 691 692 try { 694 acquireCacheLock(); 695 696 sm = (StateManager)_weakCache.get(oid); 697 if (sm == null) { 698 boolean external = false; 699 if (classType == null) { 701 classType = loadClassForOid(oid); 702 703 if (classType == null) { 704 throw new JDOUserException(I18NHelper.getMessage(messages, 706 "jdo.persistencemanagerimpl.getobjectbyid.nometadata"), new Object []{oid}); 708 } 709 external = true; 710 } 711 712 try { 713 sm = createStateManager(classType); 715 if (external) { 716 oid = internalCloneOid(oid, sm); 719 } 720 sm.setObjectId(oid); 721 setKeyFields(sm); 722 723 if (external) { 724 sm.initialize(false); 725 } else { 726 _weakCache.put(oid, sm); 728 } 729 } catch (JDOException e) { 730 throw e; 731 } catch (Exception e) { 732 throw new JDOUserException(I18NHelper.getMessage(messages, 733 "jdo.persistencemanagerimpl.fetchinstance.none"), e); } 735 } 736 } finally { 737 releaseCacheLock(); 738 } 739 740 return sm; 741 } 742 743 747 private StateManager createStateManager(Class classType) { 748 StateManager rc = _store.getStateManager(classType); 749 newInstance(rc); 750 751 return rc; 752 } 753 754 762 public Object getObjectId(Object pc) { 763 boolean debug = logger.isLoggable(Logger.FINEST); 764 765 assertIsOpen(); 766 assertActiveTransaction(true); 767 768 if (debug) { 769 Object [] items = new Object [] {Thread.currentThread(),pc, this, _jta}; 770 logger.finest("sqlstore.persistencemgr.getobjid",items); } 772 773 try { 774 assertPersistenceCapable(pc); 775 } catch (Exception e) { 776 if (debug) { 777 Object [] items = new Object [] {pc, this}; 778 logger.finest("sqlstore.persistencemgr.getobjid.notpc",items); } 780 781 return null; 782 } 783 784 StateManager sm = ((PersistenceCapable)pc).jdoGetStateManager(); 785 if (sm == null) { 786 return null; 788 789 } else if (sm.getPersistenceManagerInternal() != this) { 790 if (debug) { 791 Object [] items = new Object [] {pc, this, _jta}; 792 logger.finest("sqlstore.persistencemgr.getobjid.notpm",items); } 794 795 return null; 796 } 797 798 return internalGetObjectId(sm); 799 } 800 801 808 public Object getTransactionalInstance(Object pc) { 809 assertIsOpen(); 810 assertActiveTransaction(false); 811 if (!(pc instanceof PersistenceCapable)) { 812 return pc; 813 } 814 815 PersistenceCapable mypc = (PersistenceCapable) pc; 816 817 PersistenceManagerWrapper pmw = (PersistenceManagerWrapper) mypc.jdoGetPersistenceManager(); 819 PersistenceManagerImpl pm = (PersistenceManagerImpl) pmw.getPersistenceManager(); 820 821 if (pm == null || pm == this) { 822 return pc; 823 } 824 825 return getObjectById(pm.internalGetObjectId(mypc.jdoGetStateManager())); 826 } 827 828 837 public void makePersistent(Object pc) { 838 boolean debug = logger.isLoggable(Logger.FINEST); 839 840 if (debug) 841 { 842 Object [] items = new Object [] {Thread.currentThread(), pc, this, _jta}; 843 logger.finest("sqlstore.persistencemgr.makepersistent",items); } 845 846 if (pc == null) 847 return; 849 acquireShareLock(); 850 try { 851 assertIsOpen(); 852 assertActiveTransaction(false); 853 assertPersistenceCapable(pc); 854 internalMakePersistent((PersistenceCapable) pc); 855 if (debug) 856 { 857 Object [] items = new Object [] {pc, this, _jta}; 858 logger.finest("sqlstore.persistencemgr.makepersistent.done",items); } 860 861 } finally { 862 releaseShareLock(); 863 } 864 } 865 866 870 public void makePersistent(Object [] pcs) { 871 if (pcs == null) 872 return; 874 for (int i = 0; i < pcs.length; i++) { 875 makePersistent(pcs[i]); 876 } 877 } 878 879 public void makePersistent(Collection pcs) { 880 if (pcs == null) 881 return; 883 makePersistent(pcs.toArray()); 884 } 885 886 public void deletePersistent(Object pc) { 887 if (pc == null) 888 return; 890 acquireShareLock(); 891 892 try { 893 assertIsOpen(); 894 assertActiveTransaction(false); 895 assertPersistenceCapable(pc); 896 internalDeletePersistent((PersistenceCapable) pc); 897 } finally { 898 releaseShareLock(); 899 } 900 } 901 902 public void deletePersistent(Object [] pcs) { 903 if (pcs == null) 904 return; 906 for (int i = 0; i < pcs.length; i++) { 907 deletePersistent(pcs[i]); 908 } 909 } 910 911 public void deletePersistent(Collection pcs) { 912 if (pcs == null) 913 return; 915 deletePersistent(pcs.toArray()); 916 } 917 918 919 925 public com.sun.jdo.api.persistence.support.PersistenceManagerFactory getPersistenceManagerFactory() { 926 return persistenceManagerFactory; 927 } 928 929 void setPersistenceManagerFactory(PersistenceManagerFactory pmf) { 930 if (persistenceManagerFactory == null) 931 persistenceManagerFactory = pmf; 932 } 933 934 940 public void setUserObject(Object o) { 941 this._userObject = o; 942 } 943 944 950 public Object getUserObject() { 951 return _userObject; 952 } 953 954 962 public Properties getProperties() { 963 if (_properties == null) { 964 _properties = RuntimeVersion.getVendorProperties( 965 "/com/sun/jdo/spi/persistence/support/sqlstore/sys.properties"); } 967 return _properties; 968 } 969 970 977 public boolean getSupersedeDeletedInstance () { 978 return _supersedeDeletedInstance; 979 } 980 981 982 986 public void setSupersedeDeletedInstance (boolean flag) { 987 _supersedeDeletedInstance = flag; 989 } 990 991 1001 public boolean getRequireCopyObjectId() { 1002 return _requireCopyObjectId; 1003 } 1004 1005 1006 1016 public void setRequireCopyObjectId (boolean flag) { 1017 _requireCopyObjectId = flag; 1019 } 1020 1021 1030 public boolean getRequireTrackedSCO() { 1031 return _requireTrackedSCO; 1032 } 1033 1034 1042 public void setRequireTrackedSCO (boolean flag) { 1043 _requireTrackedSCO = flag; 1045 } 1046 1047 1052 public Class getObjectIdClass(Class cls) { 1053 PersistenceConfig config = loadPersistenceConfig(cls); 1054 return config.getOidClass(); 1055 } 1056 1057 1063 public Object newInstance(StateManager sm) { 1064 Object o = null; 1065 1066 PersistenceConfig config = sm.getPersistenceConfig(); 1067 1068 if (config == null) { 1069 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 1070 "jdo.persistencemanagerimpl.newinstance.badsm")); } 1072 1073 Class type = config.getPersistenceCapableClass(); 1074 1075 try { 1076 Constructor constr = type.getConstructor(sigSM); 1077 1078 if (constr != null) { 1079 o = constr.newInstance(new Object []{sm}); 1080 1081 sm.setPersistenceManager(this); 1083 sm.setPersistent(o); 1084 } 1085 } catch (Exception e) { 1086 throw new JDOFatalUserException(I18NHelper.getMessage(messages, 1087 "jdo.persistencemanagerimpl.assertpersistencecapable.error", type.getName()), e); 1089 } 1090 1091 return o; 1092 } 1093 1094 1108 public Object retrieve(RetrieveDesc action, ValueFetcher parameters) 1109 { 1110 acquireShareLock(); 1111 1112 try { 1113 assertActiveTransaction(true); 1114 return _store.retrieve(this, action, parameters); 1115 } finally { 1116 releaseShareLock(); 1117 } 1118 } 1119 1120 1133 public Object retrieve(RetrieveDesc action) { 1134 return retrieve(action, null); 1135 } 1136 1137 1140 public RetrieveDesc getRetrieveDesc(Class classType) { 1141 acquireShareLock(); 1142 1143 try { 1144 loadPersistenceConfig(classType); 1145 return _store.getRetrieveDesc(classType); 1146 } finally { 1147 releaseShareLock(); 1148 } 1149 } 1150 1151 1155 public RetrieveDesc getRetrieveDesc(String fieldName, Class classType) { 1156 acquireShareLock(); 1157 1158 try { 1159 loadPersistenceConfig(classType); 1160 return _store.getRetrieveDesc(fieldName, classType); 1161 } finally { 1162 releaseShareLock(); 1163 } 1164 } 1165 1166 1167 1172 public void registerInstance(StateManager sm, Object oid) { 1173 boolean debug = logger.isLoggable(Logger.FINEST); 1174 if (debug) 1175 { 1176 Object [] items = new Object [] {Thread.currentThread(), oid, this, _jta}; 1177 logger.finest("sqlstore.persistencemgr.registerinstance",items); } 1179 1180 try { 1181 acquireCacheLock(); 1182 if (debug) 1183 logger.finest("sqlstore.persistencemgr.registerinstancein_wkc"); 1185 _weakCache.put(oid, sm); 1186 1187 if (sm.needsRegisterWithVersionConsistencyCache()) { 1188 addToVersionConsistencyCache(sm); 1189 } 1190 } finally { 1191 releaseCacheLock(); 1192 } 1193 } 1194 1195 1198 public void registerInstance(StateManager sm, Object oid, 1199 boolean throwDuplicateException, 1200 boolean forceRegister) { 1201 if (oid == null) { 1202 oid = sm.getObjectId(); 1203 } 1204 1205 boolean debug = logger.isLoggable(); 1206 if (debug) { 1207 Object [] items = new Object [] {Thread.currentThread(), oid, sm, this, _jta}; 1208 logger.finest("sqlstore.persistencemgr.registerinstance",items); } 1210 1211 try { 1218 acquireCacheLock(); 1219 if (!_weakCache.containsKey(oid)) { 1220 if (debug) 1221 logger.finest("sqlstore.persistencemgr.registerinstancein_wkc"); 1223 _weakCache.put(oid, sm); 1224 } else if (throwDuplicateException) { 1225 StateManager old = (StateManager)_weakCache.get(oid); 1226 if (_supersedeDeletedInstance && old.isDeleted()) { 1227 1228 if (debug) 1229 logger.finer(I18NHelper.getMessage(messages, 1230 "sqlstore.persistencemgr.replacingdeletedinstance", oid)); 1232 old.markNotRegistered(); 1233 old.markVerifyAtDeregister(); 1234 sm.markVerifyAtDeregister(); 1235 sm.markReplacement(); 1236 1237 old.addDependency(sm); 1240 1241 _weakCache.put(oid, sm); 1245 1246 return; 1248 1249 } else { 1250 throw new JDODuplicateObjectIdException(I18NHelper.getMessage(messages, 1251 "jdo.persistencemanagerimpl.internalmakepersistent.dups"), new Object []{sm.getPersistent()}); 1253 } 1254 } 1255 1256 if (_activeTransaction && (sm.isTransactional() || forceRegister)) { 1257 if (debug) { 1258 Object [] items = new Object [] {oid,sm.getPersistent(),this, _jta}; 1259 logger.finest("sqlstore.persistencemgr.registerinstancein_txc",items); } 1261 1262 if (sm.isDirty()) { 1265 _txCache.add(sm); 1266 } 1267 1268 _flushedCache.add(sm); 1270 1271 if (sm.needsRegisterWithVersionConsistencyCache()) { 1272 addToVersionConsistencyCache(sm); 1273 } 1274 } 1275 1276 } finally { 1277 releaseCacheLock(); 1278 } 1279 } 1280 1281 public void deregisterInstance(Object oid) { 1282 boolean debug = logger.isLoggable(Logger.FINEST); 1283 if (debug) { 1284 Object [] items = new Object [] {oid,this,_jta}; 1285 logger.finest("sqlstore.persistencemgr.deregisterinstance",items); } 1287 1288 if (oid != null) { 1289 try { 1290 acquireCacheLock(); 1291 StateManager sm = (StateManager) _weakCache.remove(oid); 1292 removeFromCaches(sm); 1293 } finally { 1294 releaseCacheLock(); 1295 } 1296 } 1297 } 1298 1299 public void deregisterInstance(Object oid, StateManager sm) { 1300 boolean debug = logger.isLoggable(Logger.FINEST); 1301 if (debug) { 1302 Object [] items = new Object [] {oid,this,_jta}; 1303 logger.finest("sqlstore.persistencemgr.deregisterinstance.verify",items); } 1305 1306 try { 1307 acquireCacheLock(); 1308 Object known = _weakCache.get(oid); 1309 if (known == sm) { 1310 _weakCache.remove(oid); 1312 if (debug) 1313 logger.finest("sqlstore.persistencemgr.deregisterinstance.verified"); } 1315 1316 removeFromCaches(sm); 1317 } finally { 1318 releaseCacheLock(); 1319 } 1320 } 1321 1322 1327 private void removeFromCaches(StateManager sm) { 1328 if (sm != null) { 1329 if (_activeTransaction) { 1330 _txCache.remove(sm); 1332 _flushedCache.remove(sm); 1333 } 1334 removeFromVersionConsistencyCache(sm); 1335 } 1336 } 1337 1338 1339 1344 public void beforeCompletion() { 1345 if (logger.isLoggable(Logger.FINEST)) { 1346 logger.finest("sqlstore.persistencemgr.beforecompletion"); } 1348 1349 assertIsOpen(); 1350 assertActiveTransaction(false); 1351 _insideCommit = true; 1352 1353 prepareToUpdate(); 1354 1355 try { 1356 flushTxCache(); 1357 1358 if (!_insideFlush) { 1360 verifyFlushedCache(); 1361 } 1362 } catch (JDODataStoreException ex) { 1363 1364 cleanupVersionConsistencyCache = true; 1367 throw ex; 1368 } 1369 } 1370 1371 1379 private void prepareToUpdate() { 1380 for (int i = 0; i < _txCache.size(); i++) { 1381 StateManager sm = (StateManager)_txCache.get(i); 1382 1385 sm.prepareToUpdatePhaseI(); 1386 } 1387 1388 if (!_insideFlush) { 1390 HashSet phase3sms = new HashSet(); 1391 1392 for (Iterator iter = _flushedCache.iterator(); iter.hasNext(); ) { 1393 StateManager sm = (StateManager)iter.next(); 1394 1398 sm.prepareToUpdatePhaseII(phase3sms); 1399 } 1400 1401 Iterator iter = phase3sms.iterator(); 1402 1403 1407 while (iter.hasNext()) { 1408 StateManager sm = (StateManager) iter.next(); 1409 sm.prepareToUpdatePhaseIII(); 1410 } 1411 } 1412 } 1413 1414 1421 private void flushTxCache() { 1422 List err = flushToDataStore(_txCache); 1423 1424 if (err != null && err.size() > 0) { 1426 Iterator iter = err.iterator(); 1427 while (iter.hasNext()) { 1428 ((StateManager) iter.next()).resolveDependencies(); 1429 } 1430 err = flushToDataStore(err); 1432 } 1433 1434 if (err != null && err.size() > 0) { 1435 _transaction.setRollbackOnly(); 1436 throw new JDOUserException(I18NHelper.getMessage(messages, 1437 "jdo.persistencemanagerimpl.notprocessed"), toPCArray(err)); 1439 } 1440 } 1441 1442 1452 private void verifyFlushedCache() { 1453 Iterator iter = _flushedCache.iterator(); 1454 1455 while (iter.hasNext()) { 1456 StateManager sm = (StateManager)iter.next(); 1457 1458 if (sm.hasVersionConsistency() && !sm.verifyPersistent()) { 1459 Object [] items = { sm.getPersistent() }; 1460 1461 sm.setVerificationFailed(); 1463 throw new JDODataStoreException(I18NHelper.getMessage(messages, 1464 "jdo.persistencemanagerimpl.verificationfailed"), items); } 1466 } 1467 } 1468 1469 1479 static private List flushToDataStore(List flushList) { 1480 int size = flushList.size(); 1481 List errorList = null; 1482 1483 for (int i = 0; i < size; i++) { 1492 StateManager sm = (StateManager)flushList.get(i); 1493 StateManager smNext = 1494 (i+1 < size)? (StateManager)flushList.get(i+1) : null; 1495 sm.updatePersistent(smNext); 1496 } 1497 1498 for (int i = 0; i < size; i++) { 1499 StateManager sm = (StateManager)flushList.get(i); 1500 if (!sm.isProcessed()) { 1501 if (errorList == null) { 1502 errorList = new ArrayList(); 1503 } 1504 1505 errorList.add(sm); 1507 } 1508 } 1509 return errorList; 1510 } 1511 1512 1519 static private Object [] toPCArray(List smList) { 1520 final int size = smList.size(); 1521 if (size > 0) { 1522 List pcList = new ArrayList(size); 1523 1524 for (int i = 0; i < size; i++) { 1525 StateManager sm = (StateManager)smList.get(i); 1526 pcList.add(sm.getPersistent()); 1527 } 1528 return pcList.toArray(); 1529 } 1530 return null; 1531 } 1532 1533 1538 public void afterCompletion(int status) { 1539 assertIsOpen(); 1540 _insideCommit = true; 1541 boolean abort = ((status == Status.STATUS_ROLLEDBACK) || 1542 (status == Status.STATUS_ROLLING_BACK) || 1543 (status == Status.STATUS_MARKED_ROLLBACK)); 1544 boolean debug = false; 1545 debug = logger.isLoggable(Logger.FINEST); 1546 if (debug) 1547 logger.finest("sqlstore.persistencemgr.aftercompletion",new Boolean (abort)); 1549 boolean retainValues = _transaction.getRetainValues(); 1550 1551 for (Iterator iter = _flushedCache.iterator(); iter.hasNext(); ) { 1552 StateManager sm = (StateManager)iter.next(); 1553 if (debug) 1554 logger.finest("sqlstore.persistencemgr.aftercompletion.process",sm.getObjectId()); 1556 if (abort) { 1557 rollback(sm, retainValues); 1558 } else { 1559 commit(sm, retainValues); 1560 } 1561 } 1562 1563 _txCache.clear(); 1565 _flushedCache.clear(); 1566 1567 _insideCommit = false; 1568 cleanupVersionConsistencyCache = false; 1569 } 1570 1571 1577 private void commit(StateManager sm, boolean retainValues) { 1578 1579 if (sm.needsUpdateInVersionConsistencyCache()) { 1580 StateManager nonTxSM = lookupFromVersionConsistencyCache(sm); 1581 1582 if (null != nonTxSM) { 1583 nonTxSM.copyFields(sm); 1584 } else { 1585 addToVersionConsistencyCache(sm); 1586 } 1587 } 1588 sm.commit(retainValues); 1589 } 1590 1591 1598 private void rollback(StateManager sm, boolean retainValues) { 1599 if (cleanupVersionConsistencyCache && sm.isVerificationFailed()) { 1600 removeFromVersionConsistencyCache(sm); 1601 } 1602 sm.rollback(retainValues); 1603 } 1604 1605 1612 private StateManager addToVersionConsistencyCache(StateManager sm) { 1613 StateManager rc = null; 1614 1615 if (null != sm && sm.hasVersionConsistency()) { 1616 Class pcType = sm.getPersistent().getClass(); 1617 Object oid = sm.getObjectId(); 1618 VersionConsistencyCache vcCache = 1619 persistenceManagerFactory.getVersionConsistencyCache(); 1620 1621 if (vcCache.get(pcType, oid) == null) { 1622 StateManager nonTxSM = createStateManager(pcType); 1623 1624 nonTxSM.copyFields(sm); 1625 nonTxSM.setPersistenceManager(null); 1627 rc = vcCache.put(pcType, oid, nonTxSM); 1628 } 1629 } 1630 return rc; 1631 } 1632 1633 1640 private StateManager removeFromVersionConsistencyCache(StateManager sm) { 1641 StateManager rc = null; 1642 1643 if (null != sm && sm.hasVersionConsistency()) { 1644 Class pcType = sm.getPersistent().getClass(); 1645 Object oid = sm.getObjectId(); 1646 VersionConsistencyCache vcCache = 1647 persistenceManagerFactory.getVersionConsistencyCache(); 1648 rc = vcCache.remove(pcType, oid); 1649 1650 if (null == rc) { 1651 } 1653 } 1654 return rc; 1655 } 1656 1657 1660 public boolean initializeFromVersionConsistencyCache(StateManager sm) { 1661 boolean rc = false; 1662 StateManager nonTxSM = lookupFromVersionConsistencyCache(sm); 1663 1664 if (null != nonTxSM) { 1665 rc = true; 1666 1667 synchronized (nonTxSM) { 1670 sm.copyFields(nonTxSM); 1671 } 1672 sm.initialize(true); 1673 } 1674 return rc; 1675 } 1676 1677 1684 private StateManager lookupFromVersionConsistencyCache(StateManager sm) { 1685 StateManager rc = null; 1686 1687 if (null != sm && sm.hasVersionConsistency()) { 1688 Class pcType = sm.getPersistent().getClass(); 1689 Object oid = sm.getObjectId(); 1690 VersionConsistencyCache vcCache = 1691 persistenceManagerFactory.getVersionConsistencyCache(); 1692 1693 rc = vcCache.get(pcType, oid); 1694 } 1695 return rc; 1696 } 1697 1698 public void setStateManager(Object pc, StateManager sm) { 1699 if (pc instanceof PersistenceCapable) { 1700 ((PersistenceCapable) pc).jdoSetStateManager(sm); 1701 } 1702 1703 } 1705 1706 1707 public void setFlags(Object pc, byte flags) { 1708 if (pc instanceof PersistenceCapable) { 1709 ((PersistenceCapable) pc).jdoSetFlags(flags); 1710 } 1711 1712 } 1714 1715 public byte getFlags(Object pc) { 1716 if (pc instanceof PersistenceCapable) { 1717 return ((PersistenceCapable) pc).jdoGetFlags(); 1718 } 1719 1720 return 0; 1721 } 1723 1724 public StateManager getStateManager(Object pc) { 1725 if (pc instanceof PersistenceCapable) { 1726 return ((PersistenceCapable) pc).jdoGetStateManager(); 1727 } 1728 1729 return null; 1730 } 1732 1733 1734 public void setField(Object o, int fieldNumber, Object value) { 1735 if (o instanceof PersistenceCapable) { 1736 PersistenceCapable pc = (PersistenceCapable) o; 1737 pc.jdoSetField(fieldNumber, value); 1738 } 1739 1740 } 1742 1743 public Object getField(Object pc, int fieldNumber) { 1744 if (pc instanceof PersistenceCapable) { 1745 return ((PersistenceCapable) pc).jdoGetField(fieldNumber); 1746 } 1747 1748 return null; 1750 } 1751 1752 public void clearFields(Object pc) { 1753 if (pc instanceof PersistenceCapable) { 1754 ((PersistenceCapable) pc).jdoClear(); 1755 } 1756 } 1757 1758 1769 public Object newSCOInstance(Class type, Object owner, String fieldName) { 1770 Object obj = null; 1771 1772 if (Collection.class.isAssignableFrom(type)) { 1773 obj = this.newCollectionInstanceInternal(type, owner, fieldName, null, true, 0); 1774 } else { 1775 obj = newSCOInstanceInternal(type, owner, fieldName); 1776 1777 } 1778 1779 this.replaceSCO(fieldName, owner, obj); 1780 1781 1782 return obj; 1783 } 1784 1785 1796 public Object newSCOInstanceInternal(Class type, Object owner, String fieldName) { 1797 1798 Object obj = null; 1799 1800 if (type == java.sql.Date .class 1801 || type == com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlDate.class) { 1802 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlDate(owner, fieldName); 1803 1804 } else if (type == java.sql.Time .class 1805 || type == com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlTime.class) { 1806 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlTime(owner, fieldName); 1807 1808 } else if (type == java.sql.Timestamp .class 1809 || type == com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlTimestamp.class) { 1810 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.SqlTimestamp(owner, fieldName); 1811 1812 } else if (type == com.sun.jdo.spi.persistence.support.sqlstore.sco.Date.class 1813 || Date.class.isAssignableFrom(type)) { 1814 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.Date(owner, fieldName); 1815 1816 } else { 1817 throw new JDOUserException(I18NHelper.getMessage(messages, 1818 "jdo.persistencemanagerimpl.newscoinstance.wrongclass", type.getName())); 1820 } 1821 1822 return obj; 1823 } 1824 1825 1826 1844 public Object newCollectionInstance(Class type, Object owner, String fieldName, 1845 Class elementType, boolean allowNulls, int initialSize) { 1846 Object obj = newCollectionInstanceInternal(type, owner, fieldName, 1847 elementType, allowNulls, initialSize); 1848 1849 this.replaceSCO(fieldName, owner, obj); 1850 1851 return obj; 1852 } 1853 1854 1868 public Object newCollectionInstanceInternal(Class type, Object owner, String fieldName, 1869 Class elementType, boolean allowNulls, int initialSize) { 1870 Object obj = null; 1871 1872 if (type == HashSet.class 1875 || type == com.sun.jdo.spi.persistence.support.sqlstore.sco.HashSet.class) { 1876 if (initialSize == 0) 1877 initialSize = 101; 1878 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.HashSet( 1879 owner, fieldName, elementType, allowNulls, initialSize); 1880 1890 } else if (Set.class.isAssignableFrom(type)) { 1891 if (initialSize == 0) 1892 initialSize = 101; 1893 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.HashSet( 1894 owner, fieldName, elementType, allowNulls, initialSize); 1895 1896 } else if (Collection.class.isAssignableFrom(type)) { 1897 if (initialSize == 0) 1900 initialSize = 101; 1901 obj = new com.sun.jdo.spi.persistence.support.sqlstore.sco.HashSet( 1902 owner, fieldName, elementType, allowNulls, initialSize); 1903 1904 } else { 1905 throw new JDOUserException(I18NHelper.getMessage(messages, 1906 "jdo.persistencemanagerimpl.newscoinstance.wrongclass", type.getName())); 1908 } 1909 boolean debug = logger.isLoggable(Logger.FINEST); 1910 if (debug) 1911 logger.finest("sqlstore.persistencemgr.newcollection",obj.getClass()); 1913 return obj; 1914 } 1915 1916 1922 public void internalFlush() { 1923 acquireExclusiveLock(); 1924 1925 try { 1926 if (_optimistic == false) { 1930 1936 _insideFlush = true; 1937 beforeCompletion(); 1938 _insideCommit = false; 1939 1940 int status = _transaction.getStatus(); 1941 1942 if ((status == Status.STATUS_ROLLEDBACK) || 1943 (status == Status.STATUS_ROLLING_BACK) || 1944 (status == Status.STATUS_MARKED_ROLLBACK)) { 1945 return; } else { 1948 for (int i = 0; i < _txCache.size(); i++) { 1949 StateManager sm = (StateManager)_txCache.get(i); 1950 sm.flushed(); 1951 } 1952 } 1953 _insideFlush = false; 1954 1955 _txCache.clear(); 1957 } 1958 } finally { 1959 releaseExclusiveLock(); 1960 } 1961 } 1962 1963 1967 public synchronized void notifyStatusChange(boolean isActive) { 1968 _activeTransaction = isActive; 1969 } 1970 1971 1975 public synchronized void notifyOptimistic(boolean optimistic) { 1976 this._optimistic = optimistic; 1977 } 1978 1979 1982 public boolean isOptimisticTransaction() { 1983 boolean debug = logger.isLoggable(Logger.FINEST); 1984 if (debug) 1985 logger.finest("sqlstore.persistencemgr.isoptimistic", new Boolean (_optimistic)); 1987 return _optimistic; 1988 } 1989 1990 1991 1995 public synchronized void notifyNontransactionalRead(boolean nontransactionalRead) { 1996 this._nontransactionalRead = nontransactionalRead; 1997 } 1998 1999 2002 public boolean isNontransactionalRead() { 2003 boolean debug = logger.isLoggable(Logger.FINEST); 2004 if (debug) { 2005 logger.finest("sqlstore.persistencemgr.isnontxread",new Boolean (_nontransactionalRead)); } 2007 2008 return _nontransactionalRead; 2009 } 2010 2011 2012 2015 public boolean isActiveTransaction() { 2016 boolean debug = logger.isLoggable(Logger.FINEST); 2017 if (debug) 2018 logger.finest("sqlstore.persistencemgr.isactivetx",new Boolean (_activeTransaction)); 2020 return _activeTransaction; 2021 } 2022 2023 public PersistenceManagerWrapper getCurrentWrapper() { 2025 boolean debug = logger.isLoggable(Logger.FINEST); 2026 if (debug) { 2027 logger.finest("sqlstore.persistencemgr.getcurrentwrapper",current); } 2029 2030 return current; 2031 } 2032 2033 2034 2035 2036 2037 protected void pushCurrentWrapper(PersistenceManagerWrapper pmw) { 2039 boolean debug = logger.isLoggable(Logger.FINEST); 2040 if (debug) { 2041 Object [] items = new Object [] {current,pmw}; 2042 logger.finest("sqlstore.persistencemgr.pushcurrentwrapper",items); } 2044 2045 current = pmw; 2046 } 2047 2048 protected void popCurrentWrapper(PersistenceManagerWrapper prev) { 2050 boolean debug = logger.isLoggable(Logger.FINEST); 2051 if (debug) { 2052 Object [] items = new Object [] {current,prev}; 2053 logger.finest("sqlstore.persistencemgr.popcurrentwrapper",items); } 2055 2056 current = prev; 2057 if (!_isClosed && _jta == null && current == null) { 2058 this.close(); 2059 } 2060 } 2061 2062 2066 protected void setJTATransaction(javax.transaction.Transaction t) { 2067 if (this._jta != null) { 2068 Object [] items = new Object [] {this._jta, t}; 2069 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2070 "jdo.persistencemanagerimpl.setjtatransaction.notnulljta", items)); 2072 } 2073 this._jta = t; 2074 } 2075 2076 2077 private void registerQuery(QueryImpl q) 2078 { 2079 acquireExclusiveLock(); 2080 2081 try { 2082 queries.add(q); 2083 } finally { 2084 releaseExclusiveLock(); 2085 } 2086 } 2087 2088 2092 private void disconnectQueries() 2093 { 2094 for (Iterator i = queries.iterator(); i.hasNext();) 2095 { 2096 QueryImpl q = (QueryImpl)i.next(); 2097 q.clearPersistenceManager(); 2098 } 2099 queries.clear(); 2100 queries = null; 2101 } 2102 2103 2104 2105 2112 private void replaceSCO(String fieldName, Object owner, Object obj) { 2113 if (owner instanceof PersistenceCapable) { 2114 acquireShareLock(); 2115 2116 try { 2117 PersistenceCapable pc = (PersistenceCapable) owner; 2119 StateManager sm = pc.jdoGetStateManager(); 2120 2121 if (obj instanceof SCOCollection) { 2122 acquireFieldUpdateLock(); 2123 try { 2124 sm.replaceObjectField(fieldName, obj); 2125 } finally { 2126 releaseFieldUpdateLock(); 2127 } 2128 } else { 2129 sm.replaceObjectField(fieldName, obj); 2130 } 2131 } finally { 2132 releaseShareLock(); 2133 } 2134 } 2135 } 2136 2137 private Object internalGetObjectId(StateManager sm) { 2138 Object oid = sm.getObjectId(); 2139 2140 return internalCloneOid(oid, sm); 2142 } 2143 2144 private void internalMakePersistent(PersistenceCapable pc) { 2145 acquireFieldUpdateLock(); 2154 try { 2155 synchronized (pc) { 2156 StateManager sm = null; 2157 2158 if (pc.jdoIsPersistent()) { 2159 sm = pc.jdoGetStateManager(); 2160 2161 if (this != pc.jdoGetStateManager().getPersistenceManagerInternal()) { 2162 throw new JDOUserException(I18NHelper.getMessage(messages, 2163 "jdo.persistencemanagerimpl.another_pm"), new Object []{pc}); 2165 } 2166 } else { 2167 Class classType = pc.getClass(); 2168 loadPersistenceConfig(classType); 2169 sm = _store.getStateManager(classType); 2170 } 2171 2172 sm.makePersistent(this, pc); 2173 } 2174 } finally { 2175 releaseFieldUpdateLock(); 2176 } 2177 } 2178 2179 private void internalDeletePersistent(PersistenceCapable pc) { 2180 if (!(pc.jdoIsPersistent())) { 2181 throw new JDOException(I18NHelper.getMessage(messages, 2182 "jdo.persistencemanagerimpl.internaldeletepersistent.transient"), new Object []{pc}); 2184 } 2185 2186 StateManager sm = pc.jdoGetStateManager(); 2187 PersistenceManager pm = (PersistenceManager) sm.getPersistenceManagerInternal(); 2188 2189 if (this != pm) { 2190 throw new JDOUserException(I18NHelper.getMessage(messages, 2191 "jdo.persistencemanagerimpl.another_pm"), new Object []{pc}); 2193 } 2194 2195 if (!pc.jdoIsDeleted()) { 2196 sm.deletePersistent(); 2200 } 2201 } 2202 2203 2208 private Class loadClassForOid(Object oid) { 2209 Class oidClass = oid.getClass(); 2210 Class classType = _store.getClassByOidClass(oidClass); 2211 if (classType != null) { 2212 return classType; 2214 } 2215 2216 loadByName(oidClass.getName(), oidClass.getClassLoader()); 2219 2220 return _store.getClassByOidClass(oidClass); 2221 } 2222 2223 2229 private void loadByName(String s, ClassLoader classLoader) { 2230 int l = s.length(); 2231 2232 if (l < 4) { 2233 return; 2235 } 2236 2237 String s1 = s.substring(l - 3); 2238 2239 if (s1.equalsIgnoreCase(oidName_OID) && 2240 (s.charAt(l - 4) == '.' || s.charAt(l - 4) == '$')) { 2241 s = s.substring(0, l - 4); 2242 } else if (s1.equalsIgnoreCase(oidName_KEY)) { 2243 s = s.substring(0, l - 3); 2244 } else { 2245 return; 2246 } 2247 2248 boolean debug = logger.isLoggable(Logger.FINEST); 2249 if (debug) 2250 logger.finest("sqlstore.persistencemgr.loadingclass",s); 2252 2253 Class oidClass = null; 2254 2255 try { 2256 if (classLoader == null) { 2258 classLoader = getClass().getClassLoader(); 2259 } 2260 oidClass = Class.forName(s, true, classLoader); 2261 2262 } catch (Exception e) { 2263 throw new JDOFatalUserException(I18NHelper.getMessage(messages, 2264 "jdo.persistencemanagerimpl.loadclassforoid.wrongoidclass"), e); } 2266 2267 loadPersistenceConfig(oidClass); 2268 } 2269 2270 2273 private void assertIsOpen() { 2274 if (_isClosed) { 2275 boolean debug = logger.isLoggable(Logger.FINEST); 2276 if (debug) { 2277 logger.finest("sqlstore.persistencemgr.assertisopen",this); } 2279 throw new JDOFatalUserException(I18NHelper.getMessage(messages, 2280 "jdo.persistencemanagerimpl.assertclosed.closed")); } 2282 } 2283 2284 2287 private void assertActiveTransaction(boolean insideQuery) { 2288 boolean debug = false; 2289 2290 debug = logger.isLoggable(Logger.FINEST); 2291 2292 if (debug) { 2293 logger.finest("sqlstore.persistencemgr.assertactivetx",_transaction); } 2295 2296 if (_insideCommit || (insideQuery && _transaction.getNontransactionalRead())) 2297 return; 2298 2299 if (!_activeTransaction) { 2300 if (debug) { 2301 logger.finest("sqlstore.persistencemgr.assertactivetx.closed",this); } 2303 throw new JDOException(I18NHelper.getMessage(messages, 2304 "jdo.persistencemanagerimpl.assertactivetransaction.error")); } 2306 } 2307 2308 2311 private void assertPersistenceCapable(Object pc) { 2312 if (!(pc instanceof PersistenceCapable)) { 2313 throw new JDOException(I18NHelper.getMessage(messages, 2314 "jdo.persistencemanagerimpl.assertpersistencecapable.error", pc.getClass().getName()), new Object []{pc}); 2316 } 2317 } 2318 2319 2324 private void setKeyFields(StateManager sm) { 2325 Object o = sm.getPersistent(); 2326 if (o == null) 2327 return; 2328 2329 Object oid = sm.getObjectId(); 2330 try { 2331 PersistenceConfig config = sm.getPersistenceConfig(); 2333 Field keyFields[] = config.getKeyFields(); 2334 String keynames[] = config.getKeyFieldNames(); 2335 for (int i = 0; i < keyFields.length; i++) { 2336 Field keyField = keyFields[i]; 2337 sm.makePresent(keynames[i], keyField.get(oid)); 2338 } 2339 2340 } catch (Exception e) { 2341 boolean debug = logger.isLoggable(Logger.FINEST); 2343 if (debug) 2344 logger.finest("sqlstore.persistencemgr.setkeyfields",e); } 2346 } 2347 2348 private PersistenceConfig loadPersistenceConfig(Class classType) { 2349 return _store.getPersistenceConfig(classType); 2350 } 2351 2352 2358 private Object internalCloneOid(Object oid, StateManager sm) { 2359 if (oid == null) 2360 return null; 2361 2362 if (!_requireCopyObjectId) { 2363 return oid; 2364 } 2365 2366 boolean debug = logger.isLoggable(Logger.FINEST); 2367 2368 Object newoid = null; 2369 try { 2370 Class oidClass = oid.getClass(); 2371 newoid = oidClass.newInstance(); 2372 PersistenceConfig config = sm.getPersistenceConfig(); 2373 Field keyFields[] = config.getKeyFields(); 2374 2375 for (int i = 0; i < keyFields.length; i++) { 2377 Field keyField = keyFields[i]; 2378 keyField.set(newoid, keyField.get(oid)); 2379 } 2380 2381 } catch (Exception e) { 2382 if (debug) 2384 logger.finest("sqlstore.persistencemgr.internalcloneoid",e); newoid = null; 2386 } 2387 2388 if (debug) 2389 { 2390 Object [] items = new Object [] {oid , newoid, new Boolean ((oid == newoid))}; 2391 logger.finest("sqlstore.persistencemgr.internalcloneoid.old",items); } 2393 2394 return newoid; 2395 } 2396 2397 2398 2402 public void acquireShareLock() { 2403 if ( ! _multithreaded) { 2404 return; 2405 } 2406 2407 boolean debug = logger.isLoggable(Logger.FINEST); 2408 2409 synchronized (_readWriteLock) { 2410 if ((_readWriteCount < 0) && 2416 (_exclusiveLockHolder == Thread.currentThread())) { 2417 return; 2418 } 2419 2420 while (_readWriteCount < 0) { 2426 _waiterCount++; 2427 2428 try { 2429 if (debug) { 2430 logger.finest("sqlstore.persistencemgr.acquiresharedlock",Thread.currentThread()); } 2432 2433 _readWriteLock.wait(); 2434 } catch (InterruptedException e) { 2435 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2436 "jdo.persistencemanagerimpl.acquiresharelock.interrupted"), e); } finally { 2438 _waiterCount--; 2439 } 2440 } 2441 2442 try { 2443 assertIsOpen(); 2447 2448 } catch (JDOException ex) { 2449 if (_readWriteCount == 0 && _waiterCount > 0) { 2455 _readWriteLock.notify(); 2456 } 2457 throw ex; 2458 } 2459 2460 _readWriteCount++; 2461 if (debug) { 2462 logger.finest("sqlstore.persistencemgr.acquiresharedlock.rdwrcount", Thread.currentThread(),new Long (_readWriteCount)); 2464 } 2465 2466 if (_readWriteCount <= 0) { 2467 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2468 "jdo.persistencemanagerimpl.acquiresharelock.failed")); } 2470 } 2471 } 2472 2473 2477 public void releaseShareLock() { 2478 if ( ! _multithreaded) { 2479 return; 2480 } 2481 2482 boolean debug = logger.isLoggable(Logger.FINEST); 2483 2484 2485 synchronized (_readWriteLock) { 2486 if ((_readWriteCount < 0) && 2492 (_exclusiveLockHolder == Thread.currentThread())) { 2493 return; 2494 } 2495 2496 try { 2497 if (_readWriteCount == 0) { 2498 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2499 "jdo.persistencemanagerimpl.releasesharelock.failed")); } 2501 2502 _readWriteCount--; 2503 } finally { 2504 if ((_waiterCount > 0) && (_readWriteCount == 0)) { 2510 _readWriteLock.notify(); 2511 } 2512 2513 if (debug) { 2514 Object [] items = new Object [] {Thread.currentThread(),new Long (_readWriteCount)}; 2515 logger.finest("sqlstore.persistencemgr.releasesharedlock",items); } 2517 } 2518 } 2519 } 2520 2521 2529 public void acquireExclusiveLock() { 2530 if ( ! _multithreaded) { 2531 return; 2532 } 2533 2534 boolean debug = logger.isLoggable(Logger.FINEST); 2535 2536 synchronized (_readWriteLock) { 2537 Thread currentThread = Thread.currentThread(); 2538 2539 if (currentThread == _exclusiveLockHolder) { 2545 _readWriteCount--; 2546 } else { 2547 while (_readWriteCount != 0) { 2553 _waiterCount++; 2554 2555 try { 2556 2557 if (debug) { 2558 logger.finest("sqlstore.persistencemgr.acquireexclusivelock",currentThread); } 2560 2561 _readWriteLock.wait(); 2562 } catch (InterruptedException e) { 2563 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2564 "jdo.persistencemanagerimpl.acquireexclusivelock.interrupted"), e); } finally { 2566 _waiterCount--; 2567 } 2568 } 2569 2570 try { 2571 assertIsOpen(); 2575 2576 } catch (JDOException ex) { 2577 if (_readWriteCount == 0 && _waiterCount > 0) { 2582 _readWriteLock.notify(); 2583 } 2584 throw ex; 2585 } 2586 2587 _readWriteCount = -1; 2588 _exclusiveLockHolder = currentThread; 2589 2590 if (debug) { 2591 Object [] items = new Object [] {currentThread,new Long (_readWriteCount)}; 2592 logger.fine("sqlstore.persistencemgr.acquireexclusivelock.count",items); } 2594 2595 } 2596 } 2597 } 2598 2599 2604 public void releaseExclusiveLock() { 2605 if ( ! _multithreaded) { 2606 return; 2607 } 2608 2609 boolean debug = logger.isLoggable(Logger.FINEST); 2610 2611 2612 synchronized (_readWriteLock) { 2613 try { 2614 if (_readWriteCount >= 0) { 2615 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 2616 "jdo.persistencemanagerimpl.releaseexclusivelock.failed")); } 2618 2619 _readWriteCount++; 2620 } finally { 2621 if (debug) { 2622 Object [] items = new Object [] {Thread.currentThread(),new Long (_readWriteCount)}; 2623 logger.finest("sqlstore.persistencemgr.releaseexclusivelock",items); } 2625 2626 if (_readWriteCount == 0) { 2631 if (_waiterCount > 0) { 2632 _readWriteLock.notify(); 2633 } 2634 2635 _exclusiveLockHolder = null; 2636 } 2637 } 2638 } 2639 } 2640 2641 2644 public void acquireFieldUpdateLock() { 2645 _fieldUpdateLock.acquire(); 2646 } 2647 2648 2651 public void releaseFieldUpdateLock() { 2652 _fieldUpdateLock.release(); 2653 } 2654 2655 2658 public void acquireCacheLock() { 2659 _cacheLock.acquire(); 2660 } 2661 2662 2664 public void releaseCacheLock() { 2665 _cacheLock.release(); 2666 } 2667 2668 2669 2670 2674 static class ExtensionFilter implements FilenameFilter { 2675 private String ext; 2676 2677 public ExtensionFilter(String ext) { 2678 this.ext = ext; 2679 } 2680 2681 2687 public boolean accept(File dir, String name) { 2688 return name.endsWith(ext); 2689 } 2690 } 2691 2692} 2693 | Popular Tags |