org.javalite.activejdbc.DB Java Examples

The following examples show how to use org.javalite.activejdbc.DB. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBSpec.java    From javalite with Apache License 2.0 6 votes vote down vote up
@Before @BeforeEach
public final void openTestConnections() {

    if(!suppressDb){
        loadConfiguration("/database.properties");
        testConnectionConfigs = getTestConnectionConfigs();
        if (testConnectionConfigs.isEmpty()) {
            LOGGER.warn("no DB connections are configured, none opened");
        }else {
            testConnectionConfigs.forEach(connectionConfig -> {
                DB db = new DB(connectionConfig.getDbName());
                db.open(connectionConfig);
                if (rollback) {
                    db.openTransaction();
                }
            });
        }
    }
}
 
Example #2
Source File: DBConnectionFilter.java    From javalite with Apache License 2.0 6 votes vote down vote up
@Override
public void onException(Exception e) {        
    if(Configuration.isTesting())
        return;

    if (connectionConfigs != null && !connectionConfigs.isEmpty()) {
        for (ConnectionConfig connectionConfig : connectionConfigs) {
            DB db = new DB(connectionConfig.getDbName());
            if (db.hasConnection()) {
                if (manageTransaction) {
                    db.rollbackTransaction();
                    logDebug("Rolling back transaction due to exception: " + e);
                }
                db.close();
            }
        }
    }
}
 
Example #3
Source File: DBConnectionFilter.java    From javalite with Apache License 2.0 6 votes vote down vote up
@Override
public void after() {
    if(Configuration.isTesting())
        return;

    if (connectionConfigs != null && !connectionConfigs.isEmpty()) {
        for (ConnectionConfig connectionConfig : connectionConfigs) {
            DB db = new DB(connectionConfig.getDbName());
            if(db.hasConnection()){
                if(manageTransaction){
                    if (RequestContext.exceptionHappened()) {
                        logDebug("Skip commit transaction because already rolled back.");
                    } else {
                        db.commitTransaction();
                    }
                }
                db.close();
            }
        }
    }
}
 
Example #4
Source File: DBConnectionFilter.java    From javalite with Apache License 2.0 6 votes vote down vote up
@Override
public void before() {

    if(Configuration.isTesting())
        return;



    if (connectionConfigs.isEmpty()) {
        throw new InitException("There are no connection specs in '" + AppConfig.activeEnv() + "' environment");
    }

    for (ConnectionConfig connectionConfig : connectionConfigs) {
        DB db = new DB(connectionConfig.getDbName());
        db.open(connectionConfig);
        if(manageTransaction){
            db.openTransaction();
        }
    }
}
 
Example #5
Source File: VirtualChestRecordManager.java    From VirtualChest with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void init()
{
    this.sql = Sponge.getServiceManager().provideUnchecked(SqlService.class);
    try // TODO: the logger is annoying, so disable it anyway (although it is a dirty way)
    {
        Field field = DB.class.getDeclaredField("LOGGER");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        int m = field.getModifiers() & ~Modifier.FINAL;
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, m);

        NOPLogger l = NOPLogger.NOP_LOGGER;
        field.set(null, l);
    }
    catch (ReflectiveOperationException e)
    {
        throw new IllegalStateException(e);
    }
}
 
Example #6
Source File: DBSpecHelper.java    From javalite with Apache License 2.0 5 votes vote down vote up
public static void closeTestConnections() {
    List<ConnectionConfig> connectionConfigs = getTestConnectionConfigs();
    for (ConnectionConfig connectionConfig : connectionConfigs) {
        String dbName = connectionConfig.getDbName();
        DB db = new DB(dbName);
        if (Configuration.rollback()) {
            db.rollbackTransaction();
        }
        db.close();

    }
}
 
Example #7
Source File: DBSpec.java    From javalite with Apache License 2.0 5 votes vote down vote up
@After @AfterEach
public final void closeTestConnections() {
    if(!suppressDb){

        for (ConnectionConfig connectionConfig : testConnectionConfigs) {
            String dbName = connectionConfig.getDbName();
            DB db = new DB(dbName);
            if (rollback) {
                db.rollbackTransaction();
            }
            db.close();
        }
        clearConnectionConfigs();
    }
}
 
Example #8
Source File: DBSpec.java    From javalite with Apache License 2.0 5 votes vote down vote up
/**
 * Set to true in order  to rollback a transaction at the end of the test.
 * <p>
 *     <em>
 *     WARNING: if you set this value to false inside your test, the framework will not
 *     clean any remaining data you insert into your test database. Basically, this is a
 *     "manual mode" where you are responsible for cleaning after yourself.
 *     </em>
 * </p>
 *
 * @param rollback true to rollback transactions at the end of the test, false to not rollback.
 */
public void setRollback(boolean rollback) {
    if(!suppressDb){
        this.rollback = rollback;
        Map<String, Connection> connections = DB.connections();
        for(String name: connections.keySet()){
            try {
                boolean autocommit = !rollback;
                connections.get(name).setAutoCommit(autocommit);
            } catch (SQLException e) {
                throw new InitException(e);
            }
        }
    }
}
 
Example #9
Source File: UniquenessValidator.java    From javalite with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(Model model) {
    MetaModel metaModel = metaModelOf(model.getClass());
    if (new DB(metaModel.getDbName()).count(metaModel.getTableName(), attribute + " = ?", model.get(attribute)) > 0) {
        model.addValidator(this, attribute);
    }
}
 
Example #10
Source File: DBSpecHelper.java    From javalite with Apache License 2.0 5 votes vote down vote up
public static void openTestConnections(){
    List<ConnectionConfig> connectionConfigs = getTestConnectionConfigs();
    if(connectionConfigs.isEmpty()){
        LOGGER.warn("no DB connections are configured, none opened");
        return;
    }

    for (ConnectionConfig connectionConfig : connectionConfigs) {
        DB db = new DB(connectionConfig.getDbName());
        db.open(connectionConfig);
        if (Configuration.rollback())
            db.openTransaction();            
    }
}
 
Example #11
Source File: Tools.java    From torrenttunes-client with GNU General Public License v3.0 5 votes vote down vote up
public static final void dbInit() {
	try {
		new DB("ttc").open("org.sqlite.JDBC", "jdbc:sqlite:" + DataSources.DB_FILE(), "root", "p@ssw0rd");
	} catch (DBException e) {
		e.printStackTrace();
		dbClose();
		dbInit();
	}

}
 
Example #12
Source File: VirtualChestRecordManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void recordExecution(UUID uuid, int order, String executionPrefix, String executionSuffix)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestActionExecutionRecord(uuid, order, executionPrefix, executionSuffix).insert();
        }
    }
}
 
Example #13
Source File: VirtualChestRecordManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void recordSlotClick(UUID uuid, String menuName, int menuSlot, VirtualChestInventory.ClickStatus s, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestClickSlotRecord(uuid, menuName, menuSlot, s, p).insert();
        }
    }
}
 
Example #14
Source File: VirtualChestRecordManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void recordClose(UUID uuid, String menuName, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestCloseRecord(uuid, menuName, p).insert();
        }
    }
}
 
Example #15
Source File: VirtualChestRecordManager.java    From VirtualChest with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void recordOpen(UUID uuid, String menuName, Player p)
{
    if (!this.databaseUrl.isEmpty())
    {
        try (DB db = new DB())
        {
            db.open(this.dataSource);
            new VirtualChestOpenRecord(uuid, menuName, p).insert();
        }
    }
}
 
Example #16
Source File: DBConnectionFilter.java    From javalite with Apache License 2.0 4 votes vote down vote up
/**
 * This constructor is used to open all configured connections for a current environment.
 */
public DBConnectionFilter() {
    this.connectionConfigs = DBConfiguration.getConnectionConfigsExceptTesting(DB.DEFAULT_NAME);
}
 
Example #17
Source File: Tools.java    From torrenttunes-client with GNU General Public License v3.0 4 votes vote down vote up
public static final void dbClose() {
	new DB("ttc").close();
}