001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.romaframework.aspect.persistence.jdo;
017:
018: import java.util.Map;
019:
020: import javax.jdo.PersistenceManager;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Class to handle persistence using JPOX tool and JDO 2 technology. Every method is not atomic, since it uses the same transaction
027: * for all. Assure to call commit() or rollback() methods when finished to avoid resource consumption. JDO 2.0:
028: * http://jcp.org/en/jsr/detail?id=243 <br/> JPOX: http://www.jpox.org
029: *
030: * @author Luca Garulli (luca.garulli@assetdata.it)
031: */
032: public class JDOTxPersistenceAspect extends JDOBasePersistenceAspect {
033:
034: protected PersistenceManager contextManager;
035: private static Log log = LogFactory
036: .getLog(JDOTxPersistenceAspect.class);
037:
038: public JDOTxPersistenceAspect(OIDManager iOIDManager) {
039: super (iOIDManager);
040: }
041:
042: public JDOTxPersistenceAspect(JDOBasePersistenceAspect iSource) {
043: super (iSource);
044: }
045:
046: public JDOTxPersistenceAspect(Map<String, String> iConfiguration) {
047: super (iConfiguration);
048: }
049:
050: @Override
051: protected void init() {
052: contextManager = JDOPersistenceHelper
053: .getPersistenceManager(factory);
054: contextManager.currentTransaction().begin();
055: contextManager.currentTransaction().setRetainValues(true);
056:
057: strategy = STRATEGY_STANDARD;
058: }
059:
060: @Override
061: public PersistenceManager getPersistenceManager() {
062: return contextManager;
063: }
064:
065: @Override
066: protected void beginOperation(PersistenceManager iManager) {
067: }
068:
069: @Override
070: protected void endOperation(PersistenceManager iManager) {
071: }
072:
073: @Override
074: protected void closeOperation(PersistenceManager iManager) {
075: }
076:
077: public void commit() {
078: log.info("[JDOTxPersistenceAspect.commit]");
079:
080: contextManager.currentTransaction().commit();
081: JDOPersistenceHelper.closeManager(contextManager);
082: }
083:
084: public void rollback() {
085: log.info("[JDOTxPersistenceAspect.rollback]");
086:
087: contextManager.currentTransaction().rollback();
088: JDOPersistenceHelper.closeManager(contextManager);
089: }
090:
091: public void close() {
092: if (contextManager.isClosed())
093: return;
094:
095: if (contextManager.currentTransaction().isActive())
096: rollback();
097: }
098:
099: @Override
100: protected void finalize() throws Throwable {
101: JDOPersistenceHelper.closeManager(contextManager);
102: }
103: }
|