public DbSqlSessionFactory createDbSqlSessionFactory() { return new DbSqlSessionFactory(); }
@Override public Session openSession(CommandContext commandContext) { DbSqlSession dbSqlSession = createDbSqlSession(); if (getDatabaseSchema() != null && getDatabaseSchema().length() > 0) { try { dbSqlSession.getSqlSession().getConnection().setSchema(getDatabaseSchema()); } catch (SQLException e) { throw new FlowableException("Could not set database schema on connection", e); } } if (getDatabaseCatalog() != null && getDatabaseCatalog().length() > 0) { try { dbSqlSession.getSqlSession().getConnection().setCatalog(getDatabaseCatalog()); } catch (SQLException e) { throw new FlowableException("Could not set database catalog on connection", e); } } if (dbSqlSession.getSqlSession().getConnection() == null) { throw new FlowableException("Invalid dbSqlSession: no active connection found"); } return dbSqlSession; }
protected void defaultInitDbSqlSessionFactoryEntitySettings(List<Class<? extends Entity>> insertOrder, List<Class<? extends Entity>> deleteOrder) { for (Class<? extends Entity> clazz : insertOrder) { dbSqlSessionFactory.getInsertionOrder().add(clazz); if (isBulkInsertEnabled) { dbSqlSessionFactory.getBulkInserteableEntityClasses().add(clazz); } } for (Class<? extends Entity> clazz : deleteOrder) { dbSqlSessionFactory.getDeletionOrder().add(clazz); } }
DbSqlSessionFactory dbSqlSessionFactory = dbSqlSession.getDbSqlSessionFactory(); if (!dbSqlSession.getDbSqlSessionFactory().isTablePrefixIsSchema()) { tableName = prependDatabaseTablePrefix(tableName); connection = dbSqlSession.getSqlSession().getConnection(); DatabaseMetaData databaseMetaData = connection.getMetaData(); ResultSet tables = null; String catalog = dbSqlSession.getConnectionMetadataDefaultCatalog(); if (dbSqlSessionFactory.getDatabaseCatalog() != null && dbSqlSessionFactory.getDatabaseCatalog().length() > 0) { catalog = dbSqlSessionFactory.getDatabaseCatalog(); String schema = dbSqlSession.getConnectionMetadataDefaultSchema(); if (dbSqlSessionFactory.getDatabaseSchema() != null && dbSqlSessionFactory.getDatabaseSchema().length() > 0) { schema = dbSqlSessionFactory.getDatabaseSchema(); String databaseType = dbSqlSessionFactory.getDatabaseType(); tables = databaseMetaData.getTables(catalog, schema, tableName, JDBC_METADATA_TABLE_TYPES); return tables.next(); } finally { try { tables.close(); } catch (Exception e) { LOGGER.error("Error closing meta data tables", e); throw new FlowableException("couldn't check if tables are already present using metadata: " + e.getMessage(), e);
protected void flushBulkInsert(Collection<Entity> entities, Class<? extends Entity> clazz) { String insertStatement = dbSqlSessionFactory.getBulkInsertStatement(clazz); insertStatement = dbSqlSessionFactory.mapStatement(insertStatement); if (insertStatement == null) { throw new FlowableException("no insert statement for " + entities.iterator().next().getClass() + " in the ibatis mapping files"); } Iterator<Entity> entityIterator = entities.iterator(); Boolean hasRevision = null; while (entityIterator.hasNext()) { List<Entity> subList = new ArrayList<>(); int index = 0; while (entityIterator.hasNext() && index < dbSqlSessionFactory.getMaxNrOfStatementsInBulkInsert()) { Entity entity = entityIterator.next(); subList.add(entity); if (hasRevision == null) { hasRevision = entity instanceof HasRevision; } index++; } sqlSession.insert(insertStatement, subList); } if (hasRevision != null && hasRevision) { entityIterator = entities.iterator(); while (entityIterator.hasNext()) { incrementRevision(entityIterator.next()); } } }
protected void flushDeleteEntities(Class<? extends Entity> entityClass, Collection<Entity> entitiesToDelete) { for (Entity entity : entitiesToDelete) { String deleteStatement = dbSqlSessionFactory.getDeleteStatement(entity.getClass()); deleteStatement = dbSqlSessionFactory.mapStatement(deleteStatement); if (deleteStatement == null) { throw new FlowableException("no delete statement for " + entity.getClass() + " in the ibatis mapping files"); } // It only makes sense to check for optimistic locking exceptions // for objects that actually have a revision if (entity instanceof HasRevision) { int nrOfRowsDeleted = sqlSession.delete(deleteStatement, entity); if (nrOfRowsDeleted == 0) { throw new FlowableOptimisticLockingException(entity + " was updated by another transaction concurrently"); } } else { sqlSession.delete(deleteStatement, entity); } } }
protected void flushDeletes() { if (deletedObjects.size() == 0 && bulkDeleteOperations.size() == 0) { return; } // Handle in entity dependency order for (Class<? extends Entity> entityClass : dbSqlSessionFactory.getDeletionOrder()) { if (deletedObjects.containsKey(entityClass)) { flushDeleteEntities(entityClass, deletedObjects.get(entityClass).values()); deletedObjects.remove(entityClass); } flushBulkDeletes(entityClass); } // Next, in case of custom entities or we've screwed up and forgotten some entity if (deletedObjects.size() > 0) { for (Class<? extends Entity> entityClass : deletedObjects.keySet()) { flushDeleteEntities(entityClass, deletedObjects.get(entityClass).values()); flushBulkDeletes(entityClass); } } deletedObjects.clear(); }
public void executeMandatorySchemaResource(String operation, String component) { String databaseType = getDbSqlSession().getDbSqlSessionFactory().getDatabaseType(); executeSchemaResource(operation, component, getResourceForDbOperation(operation, operation, component, databaseType), false); }
@SuppressWarnings({ "rawtypes" }) public List selectListWithRawParameterNoCacheCheck(String statement, ListQueryParameterObject parameter) { parameter.setDatabaseType(dbSqlSessionFactory.getDatabaseType()); return selectListWithRawParameter(statement, parameter, false); }
protected String prependDatabaseTablePrefix(String tableName) { return getDbSqlSession().getDbSqlSessionFactory().getDatabaseTablePrefix() + tableName; }
protected void initDbSqlSessionFactory(AbstractEngineConfiguration engineConfiguration, AbstractEngineConfiguration targetEngineConfiguration) { DbSqlSessionFactory dbSqlSessionFactory = engineConfiguration.getDbSqlSessionFactory(); targetEngineConfiguration.setDbSqlSessionFactory(engineConfiguration.getDbSqlSessionFactory()); targetEngineConfiguration.setSqlSessionFactory(engineConfiguration.getSqlSessionFactory()); if (getEntityInsertionOrder() != null) { for (Class<? extends Entity> clazz : getEntityInsertionOrder()) { dbSqlSessionFactory.getInsertionOrder().add(clazz); } } if (getEntityDeletionOrder() != null) { for (Class<? extends Entity> clazz : getEntityDeletionOrder()) { dbSqlSessionFactory.getDeletionOrder().add(clazz); } } }
public boolean isOracle() { return getDatabaseType().equals("oracle"); }
protected void dbSchemaUpgrade(final String component, final int currentDatabaseVersionsIndex) { FlowableVersion version = FlowableVersions.FLOWABLE_VERSIONS.get(currentDatabaseVersionsIndex); String dbVersion = version.getMainVersion(); LOGGER.info("upgrading flowable {} schema from {} to {}", component, dbVersion, FlowableVersions.CURRENT_VERSION); // Actual execution of schema DDL SQL for (int i = currentDatabaseVersionsIndex + 1; i < FlowableVersions.FLOWABLE_VERSIONS.size(); i++) { String nextVersion = FlowableVersions.FLOWABLE_VERSIONS.get(i).getMainVersion(); // Taking care of -SNAPSHOT version in development if (nextVersion.endsWith("-SNAPSHOT")) { nextVersion = nextVersion.substring(0, nextVersion.length() - "-SNAPSHOT".length()); } dbVersion = dbVersion.replace(".", ""); nextVersion = nextVersion.replace(".", ""); LOGGER.info("Upgrade needed: {} -> {}. Looking for schema update resource for component '{}'", dbVersion, nextVersion, component); String databaseType = getDbSqlSession().getDbSqlSessionFactory().getDatabaseType(); executeSchemaResource("upgrade", component, getResourceForDbOperation("upgrade", "upgradestep." + dbVersion + ".to." + nextVersion, component, databaseType), true); // To avoid having too much similar scripts, for upgrades the 'all' database is supported and executed for every database type executeSchemaResource("upgrade", component, getResourceForDbOperation("upgrade", "upgradestep." + dbVersion + ".to." + nextVersion, component, "all"), true); dbVersion = nextVersion; } }
@SuppressWarnings("rawtypes") public List selectList(String statement, ListQueryParameterObject parameter) { parameter.setDatabaseType(dbSqlSessionFactory.getDatabaseType()); return selectListWithRawParameter(statement, parameter); }
public boolean isMysql() { return getDatabaseType().equals("mysql"); }
protected void dbSchemaUpgradeUntil6120(final String component, final int currentDatabaseVersionsIndex) { FlowableVersion version = FlowableVersions.FLOWABLE_VERSIONS.get(currentDatabaseVersionsIndex); String dbVersion = version.getMainVersion(); LOGGER.info("upgrading flowable {} schema from {} to {}", component, dbVersion, FlowableVersions.LAST_V6_VERSION_BEFORE_SERVICES); // Actual execution of schema DDL SQL for (int i = currentDatabaseVersionsIndex + 1; i < FlowableVersions.getFlowableVersionIndexForDbVersion(FlowableVersions.LAST_V6_VERSION_BEFORE_SERVICES); i++) { String nextVersion = FlowableVersions.FLOWABLE_VERSIONS.get(i).getMainVersion(); // Taking care of -SNAPSHOT version in development if (nextVersion.endsWith("-SNAPSHOT")) { nextVersion = nextVersion.substring(0, nextVersion.length() - "-SNAPSHOT".length()); } dbVersion = dbVersion.replace(".", ""); nextVersion = nextVersion.replace(".", ""); LOGGER.info("Upgrade needed: {} -> {}. Looking for schema update resource for component '{}'", dbVersion, nextVersion, component); String databaseType = getDbSqlSession().getDbSqlSessionFactory().getDatabaseType(); executeSchemaResource("upgrade", component, getResourceForDbOperation("upgrade", "upgradestep." + dbVersion + ".to." + nextVersion, component, databaseType), true); // To avoid having too much similar scripts, for upgrades the 'all' database is supported and executed for every database type executeSchemaResource("upgrade", component, getResourceForDbOperation("upgrade", "upgradestep." + dbVersion + ".to." + nextVersion, component, "all"), true); dbVersion = nextVersion; } }