All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.fw.common.mithra.list.TransactionalAdhocFastList Maven / Gradle / Ivy

There is a newer version: 18.1.0
Show newest version
/*
 Copyright 2016 Goldman Sachs.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

package com.gs.fw.common.mithra.list;

import com.gs.fw.common.mithra.*;
import com.gs.fw.common.mithra.extractor.EmbeddedValueExtractor;
import com.gs.fw.common.mithra.attribute.*;
import com.gs.fw.common.mithra.util.Time;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Iterator;
import java.util.Date;
import java.math.BigDecimal;


public class TransactionalAdhocFastList extends AdhocFastList
{
    private transient DependentRelationshipAddHandler addHandler = null;

    public TransactionalAdhocFastList()
    {
        //for Externalizable
    }

    public TransactionalAdhocFastList(DelegatingList originalList)
    {
        super(originalList);
    }

    public TransactionalAdhocFastList(DelegatingList originalList, Collection c)
    {
        super(originalList, c);
    }

    public TransactionalAdhocFastList(DelegatingList originalList, int initialCapacity)
    {
        super(originalList, initialCapacity);
    }

    @Override
    protected void addHook(Object obj)
    {
        super.addHook(obj);
        if (this.addHandler != null)
        {
            this.addHandler.addRelatedObject((MithraTransactionalObject) obj);
        }
    }

    public boolean addAll(int index, Collection c)
    {
        boolean result = super.addAll(index, c);
        if (this.addHandler !=  null)
        {
            for(Iterator it = c.iterator(); it.hasNext(); )
            {
                this.addHandler.addRelatedObject((MithraTransactionalObject) it.next());
            }
        }
        return result;
    }

    public boolean addAll(Collection c)
    {
        boolean result = super.addAll(c);
        if (this.addHandler !=  null)
        {
            for(Iterator it = c.iterator(); it.hasNext(); )
            {
                this.addHandler.addRelatedObject((MithraTransactionalObject) it.next());
            }
        }
        return result;
    }

    public void insertAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new InsertAllTransactionalCommand(this));
    }

    public void bulkInsertAll()
    {
        MithraManager manager = MithraManagerProvider.getMithraManager();
        manager.executeTransactionalCommand(new InsertAllTransactionalCommand(this, 100), 0);
    }

    public void cascadeInsertAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new CascadeInsertAllTransactionalCommand(this));
    }

    public void cascadeInsertAllUntil(Timestamp exclusiveUntil)
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new CascadeInsertAllUntilTransactionalCommand(this, exclusiveUntil));
    }

    public void deleteAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new DeleteAllOneByOneCommand(this));
    }

    public void deleteAllInBatches(int batchSize)
    {
        verifyNotInTransaction();
        final int[] bSize = new int[1];
        bSize[0] = batchSize;
        TransactionalCommand command = new DeleteAllInBatchesForNonOperationListCommand(this, bSize);
        executeCommandInBatches(bSize, command);
    }

    private void executeCommandInBatches(int[] batchSize, TransactionalCommand command)
    {
        int affectedRows;
        long sleepTime = 2000;

        int retryCount = 5;
        do
        {
            try
            {
                do
                {
                   affectedRows = (Integer) MithraManagerProvider.getMithraManager().executeTransactionalCommand(command, 0);
                }
                while(affectedRows >= batchSize[0]);

                retryCount = 0;
            }
            catch (Throwable throwable)
            {
                retryCount = handleRetryException(throwable, retryCount);
                try
                {
                    Thread.sleep(sleepTime);
                    sleepTime *= 2;
                }
                catch(InterruptedException e)
                {
                    //log something here
                }
            }
        }
        while(retryCount > 0);
    }

    private void verifyNotInTransaction()
    {
        if (MithraManagerProvider.getMithraManager().isInTransaction())
        {
            throw new MithraTransactionException("deleteAllInBatches cannot be called from another transaction!");
        }
    }

    private int handleRetryException(Throwable t, int retryCount)
    {
       --retryCount;
       if(retryCount == 0)
       {
           throw new MithraBusinessException("Retried the transaction but still failing. This could be happening due to issues with the transaction log");
       }
       return retryCount;
    }


    public void cascadeDeleteAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new CascadeDeleteAllOneByOneCommand(this));
    }

    public void terminateAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new TerminateAllTransactionalCommand(this));
    }

    public void cascadeTerminateAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new CascadeTerminateAllTransactionalCommand(this));
    }

    public void cascadeTerminateAllUntil(Timestamp exclusiveUntil)
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new CascadeTerminateAllUntilTransactionalCommand(this, exclusiveUntil));
    }

    public void purgeAll()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new PurgeAllForNonOperationListCommand(this) );

    }


    public void purgeAllInBatches(int batchSize)
    {
        throw new RuntimeException("Not implemented");
    }

    public void copyDetachedValuesToOriginalOrInsertIfNewOrDeleteIfRemoved()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new UpdateOriginalObjectsFromDetachedList(this, null, null));
    }

    public void zCopyDetachedValuesDeleteIfRemovedOnly()
    {
        // nothing to do
    }

    public void cascadeUpdateInPlaceBeforeTerminate()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new InPlaceUpdateOriginalObjectsBeforeTerminate(this, null, null));
    }

    public void copyDetachedValuesToOriginalOrInsertIfNewOrTerminateIfRemoved()
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new UpdateOriginalObjectsFromDetachedList(this, null, null));
    }

    public void copyDetachedValuesToOriginalUntilOrInsertIfNewUntilOrTerminateIfRemoved(Timestamp exclusiveUntil)
    {
        MithraManagerProvider.getMithraManager().executeTransactionalCommand(new UpdateOriginalObjectsUntilFromDetachedList(this, null, null, exclusiveUntil));
    }

    public void zSetAddHandler(DependentRelationshipAddHandler addHandler)
    {
        this.addHandler = addHandler;
    }

    public void zSetRemoveHandler(DependentRelationshipRemoveHandler removeHandler)
    {
        throw new RuntimeException("Can't set remove handler for non-operation based list");
    }

    public void setBoolean(BooleanAttribute attr, boolean newValue)
    {
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy