Search in sources :

Example 6 with DbOomManager

use of jodd.db.oom.DbOomManager in project jodd by oblac.

the class DefaultAppCore method startDb.

/**
	 * Initializes database. First, creates connection pool.
	 * and transaction manager. Then, Jodds DbOomManager is
	 * configured. It is also configured automagically, by scanning
	 * the class path for entities.
	 */
@SuppressWarnings("unchecked")
protected void startDb() {
    if (!useDatabase) {
        log.info("database is not used");
        return;
    }
    log.info("database initialization");
    // connection pool
    connectionProvider = createConnectionProvider();
    petite.addBean(PETITE_DBPOOL, connectionProvider);
    connectionProvider.init();
    checkConnectionProvider();
    // transactions manager
    jtxManager = createJtxTransactionManager(connectionProvider);
    jtxManager.setValidateExistingTransaction(true);
    AnnotationTxAdviceManager annTxAdviceManager = new AnnotationTxAdviceManager(jtxManager, jtxScopePattern);
    annTxAdviceManager.registerAnnotations(jtxAnnotations);
    AnnotationTxAdviceSupport.manager = annTxAdviceManager;
    DbSessionProvider sessionProvider = new DbJtxSessionProvider(jtxManager);
    // global settings
    DbManager dbManager = DbManager.getInstance();
    dbManager.setConnectionProvider(connectionProvider);
    dbManager.setSessionProvider(sessionProvider);
    petite.addBean(PETITE_DB, dbManager);
    DbOomManager dbOomManager = DbOomManager.getInstance();
    petite.addBean(PETITE_DBOOM, dbOomManager);
    // automatic configuration
    registerDbEntities(dbOomManager);
}
Also used : AnnotationTxAdviceManager(jodd.jtx.proxy.AnnotationTxAdviceManager) DbJtxSessionProvider(jodd.db.jtx.DbJtxSessionProvider) DbSessionProvider(jodd.db.DbSessionProvider) DbOomManager(jodd.db.oom.DbOomManager) DbManager(jodd.db.DbManager)

Example 7 with DbOomManager

use of jodd.db.oom.DbOomManager in project jodd by oblac.

the class DbIdGenerator method nextId.

/**
	 * Returns next ID for given entity type.
	 * On the first call, it finds the max value of all IDs and stores it.
	 * On later calls, stored id is incremented and returned.
	 */
public synchronized long nextId(Class entityType) {
    MutableLong lastId = entityIdsMap.get(entityType);
    if (lastId == null) {
        DbOomManager dbOomManager = DbOomManager.getInstance();
        DbEntityDescriptor ded = dbOomManager.lookupType(entityType);
        String tableName = ded.getTableName();
        String idColumn = ded.getIdColumnName();
        DbOomQuery dbOomQuery = query("select max(" + idColumn + ") from " + tableName);
        long lastLong = dbOomQuery.autoClose().executeCount();
        if (log.isDebugEnabled()) {
            log.debug("Last id for " + entityType.getName() + " is " + lastLong);
        }
        lastId = new MutableLong(lastLong);
        entityIdsMap.put(entityType, lastId);
    }
    lastId.value++;
    return lastId.value;
}
Also used : MutableLong(jodd.mutable.MutableLong) DbOomQuery(jodd.db.oom.DbOomQuery) DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Aggregations

DbOomManager (jodd.db.oom.DbOomManager)7 DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)4 Before (org.junit.Before)2 DbManager (jodd.db.DbManager)1 DbQuery (jodd.db.DbQuery)1 DbSessionProvider (jodd.db.DbSessionProvider)1 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)1 DbOomException (jodd.db.oom.DbOomException)1 DbOomQuery (jodd.db.oom.DbOomQuery)1 AnnotationTxAdviceManager (jodd.jtx.proxy.AnnotationTxAdviceManager)1 MutableLong (jodd.mutable.MutableLong)1