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

com.ning.billing.util.entity.dao.EntitySqlDaoTransactionalJdbiWrapper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Ning, Inc.
 *
 * Ning licenses this file to you 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.ning.billing.util.entity.dao;

import org.skife.jdbi.v2.IDBI;
import org.skife.jdbi.v2.Transaction;
import org.skife.jdbi.v2.TransactionIsolationLevel;
import org.skife.jdbi.v2.TransactionStatus;

import com.ning.billing.clock.Clock;
import com.ning.billing.util.cache.CacheControllerDispatcher;
import com.ning.billing.util.dao.NonEntityDao;
import com.ning.billing.util.entity.Entity;

/**
 * Transaction manager for EntitySqlDao queries
 */
public class EntitySqlDaoTransactionalJdbiWrapper {

    private final IDBI dbi;
    private final Clock clock;
    private final CacheControllerDispatcher cacheControllerDispatcher;
    private final NonEntityDao nonEntityDao;

    public EntitySqlDaoTransactionalJdbiWrapper(final IDBI dbi, final Clock clock, final CacheControllerDispatcher cacheControllerDispatcher, final NonEntityDao nonEntityDao) {
        this.dbi = dbi;
        this.clock = clock;
        this.cacheControllerDispatcher = cacheControllerDispatcher;
        this.nonEntityDao = nonEntityDao;
    }

    class JdbiTransaction, E extends Entity> implements Transaction> {

        private final EntitySqlDaoTransactionWrapper entitySqlDaoTransactionWrapper;

        JdbiTransaction(final EntitySqlDaoTransactionWrapper entitySqlDaoTransactionWrapper) {
            this.entitySqlDaoTransactionWrapper = entitySqlDaoTransactionWrapper;
        }

        @Override
        public ReturnType inTransaction(final EntitySqlDao transactionalSqlDao, final TransactionStatus status) throws Exception {
            final EntitySqlDaoWrapperFactory factoryEntitySqlDao = new EntitySqlDaoWrapperFactory(transactionalSqlDao, clock, cacheControllerDispatcher, nonEntityDao);
            return entitySqlDaoTransactionWrapper.inTransaction(factoryEntitySqlDao);
        }
    }

    // To handle warnings only
    interface InitialEntitySqlDao extends EntitySqlDao, Entity> {}

    /**
     * @param entitySqlDaoTransactionWrapper transaction to execute
     * @param                    object type to return from the transaction
     * @return result from the transaction fo type ReturnType
     */
    public  ReturnType execute(final EntitySqlDaoTransactionWrapper entitySqlDaoTransactionWrapper) {
        final EntitySqlDao, Entity> entitySqlDao = dbi.onDemand(InitialEntitySqlDao.class);
        return entitySqlDao.inTransaction(TransactionIsolationLevel.READ_COMMITTED, new JdbiTransaction, Entity>(entitySqlDaoTransactionWrapper));
    }

    public , E extends Entity, T extends EntitySqlDao> T onDemand(final Class sqlObjectType) {
        return dbi.onDemand(sqlObjectType);
    }

    /**
     * @param entitySqlDaoTransactionWrapper transaction to execute
     * @param                    object type to return from the transaction
     * @param                             checked exception which can be thrown from the transaction
     * @return result from the transaction fo type ReturnType
     */
    public  ReturnType execute(final Class exception, final EntitySqlDaoTransactionWrapper entitySqlDaoTransactionWrapper) throws E {
        try {
            return execute(entitySqlDaoTransactionWrapper);
        } catch (RuntimeException e) {
            if (e.getCause() != null && e.getCause().getClass().isAssignableFrom(exception)) {
                throw (E) e.getCause();
            } else if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
                throw (RuntimeException) e.getCause();
            } else {
                throw e;
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy