AnyLogic
Expand
Font size

public class ModelDatabase
extends ModelDBConnectivity
This class represents the built-in AnyLogic Model Database. Almost all the methods of this class are internal. The database object may be accessed from getEngine().getModelDatabase() and may be used for import / export with external data sources.
Author:
AnyLogic North America, LLC https://anylogic.com

Field Summary

Constructor Summary

ConstructorDescription
ModelDatabase(Engine engine, ModelProperties modelProperties, DatabaseLogState logState)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.

Method Summary

Modifier and TypeMethodDescription
ResultSetexecute(String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
voidexportToExternalDB(String sourceTableName, Connection targetConnection, String targetTableName, boolean clearTargetTable, boolean autoCommit)
Exports a single table data from this database to an external database.
voidimportFromExternalDB(Connection sourceConnection, String sourceTableName, String targetTableName, boolean clearTargetTable, boolean autoCommit)
Imports a single table data from an external database.
ModelDBConfigurationinitConfiguration()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
booleanisLoggingOn(LoggingType loggingType) 
voidlog(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
voidresetBeforeStart()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetselect(String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetselectFrom(String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetwrapResultSet(ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

Methods inherited from class com.anylogic.engine.database.ModelDBConnectivity

disconnect, getConnection, getCreateTableSQL, getDSLConfiguration, getLogStatements, getQuerySupport, setConfiguration

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

ModelDatabase

@AnyLogicInternalAPI
public ModelDatabase(Engine engine,
 ModelProperties modelProperties,
 DatabaseLogState logState)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.

Method Details

initConfiguration

@AnyLogicInternalAPI
public ModelDBConfiguration initConfiguration()
Description copied from class: ModelDBConnectivity
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Overrides:
initConfiguration in class ModelDBConnectivity
Returns:
default implementation does nothing and returns null
See Also:

resetBeforeStart

@AnyLogicInternalAPI
public void resetBeforeStart()
                      throws Exception
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
Exception

isLoggingOn

public boolean isLoggingOn(LoggingType loggingType)

log

@AnyLogicInternalAPI
public void log(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

select

@AnyLogicInternalAPI
public ResultSet select(String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

execute

@AnyLogicInternalAPI
public ResultSet execute(String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

selectFrom

@AnyLogicInternalAPI
public ResultSet selectFrom(String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

importFromExternalDB

public void importFromExternalDB(Connection sourceConnection,
 String sourceTableName,
 String targetTableName,
 boolean clearTargetTable,
 boolean autoCommit)
Imports a single table data from an external database. Column types should match or be convertible.
Parameters:
sourceConnection - Connection object for external database to copy data from
sourceTableName - name of the table in external database to copy data from
targetTableName - table in this database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = modelDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

exportToExternalDB

public void exportToExternalDB(String sourceTableName,
 Connection targetConnection,
 String targetTableName,
 boolean clearTargetTable,
 boolean autoCommit)
Exports a single table data from this database to an external database. Column types should match or be convertible.
When exporting to Database.getConnection(), depending on the external database implementation, the data may be actually saved when the model is stopped (when Engine will automatically call Database.disconnect() at the end). For custom experiment this requires calling engine.stop() at the end.
Parameters:
sourceTableName - table in this database to copy data from
targetConnection - Connection object for external database to copy data to
targetTableName - table in external database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = targetDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

wrapResultSet

@AnyLogicInternalAPI
public ResultSet wrapResultSet(ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.