Package nl.cwi.monetdb.embedded.env
Class MonetDBEmbeddedConnection
java.lang.Object
nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
JDBCEmbeddedConnection
public class MonetDBEmbeddedConnection extends Object implements Closeable
A single connection to a MonetDB database instance. Communication between Java and native C is done via JNI.
Note: You can have only one Embedded MonetDB database running per JVM process.
Note: You can have only one Embedded MonetDB database running per JVM process.
- Author:
- Pedro Ferreira
-
Field Summary
Fields Modifier and Type Field Description protected longconnectionPointerThe C connection pointer. -
Constructor Summary
Constructors Modifier Constructor Description protectedMonetDBEmbeddedConnection(long connectionPointer) -
Method Summary
Modifier and Type Method Description booleancheckIfTableExists(String schemaName, String tableName)Check if a table exists in the database.voidclose()Shuts down this connection.protected voidcloseConnectionImplementation()When the database shuts down, this method is called insteadvoidcommit()Commits the current transaction.QueryResultSetexecuteQuery(String query)Executes a SQL query with a result set.intexecuteUpdate(String query)Executes a SQL query without a result set, returning the number of rows it affected.booleangetAutoCommit()Retrieves the current auto-commit mode for this connection.MonetDBTablegetMonetDBTable(String tableName)Retrieves a MonetDB Table in the current schema for further operations.MonetDBTablegetMonetDBTable(String schemaName, String tableName)Retrieves a database table for further operations on it such as appending data.longgetRandomIdentifier()Retrieves a long number randomly generated, used to identify the connection uniquely.StringgetSchema()Retrieves the current schema set on the connection.booleanisClosed()Tells if the connection has been closed or not.QueryResultSetlistTables(boolean listSystemTables)Performs a listing of the existing tables with schemas.MonetDBEmbeddedPreparedStatementprepareStatement(String query)Starts a prepared statement.voidreleaseSavepoint(Savepoint savepoint)Removes the given Savepoint object from the current transaction.voidremoveTable(String schemaName, String tableName)Deletes a table if exists in the database.voidrollback()Rollbacks the current transaction.voidrollback(Savepoint savepoint)Undoes all changes made after the given Savepoint object was set.voidsetAutoCommit(boolean autoCommit)Sets this connection's auto-commit mode to the given state.SavepointsetSavepoint()Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it.SavepointsetSavepoint(String name)Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it.voidsetSchema(String newSchema)Sets the current schema for this connection.voidstartTransaction()Begins a transaction.StringtoString()A brief description of the connection.
-
Field Details
-
connectionPointer
protected volatile long connectionPointerThe C connection pointer.
-
-
Constructor Details
-
MonetDBEmbeddedConnection
protected MonetDBEmbeddedConnection(long connectionPointer)
-
-
Method Details
-
getRandomIdentifier
public long getRandomIdentifier()Retrieves a long number randomly generated, used to identify the connection uniquely.- Returns:
- The random identifier
-
isClosed
public boolean isClosed()Tells if the connection has been closed or not.- Returns:
- A boolean indicating if the connection has been cleaned or not
-
getSchema
Retrieves the current schema set on the connection.- Returns:
- A Java String with the name of the schema
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
setSchema
Sets the current schema for this connection.- Parameters:
newSchema- Java String with the name of the schema- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
getAutoCommit
Retrieves the current auto-commit mode for this connection.- Returns:
- the current state of this Connection object's auto-commit mode
- Throws:
MonetDBEmbeddedException- if a database access error occurs
-
setAutoCommit
Sets this connection's auto-commit mode to the given state. If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback. By default, new connections are in auto-commit mode. NOTE: If this method is called during a transaction, the transaction is committed.- Parameters:
autoCommit- true to enable auto-commit mode; false to disable it- Throws:
MonetDBEmbeddedException- if a database access error occurs
-
startTransaction
Begins a transaction. WARNING Starting a transaction will turn the autocommit mode off!- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
commit
Commits the current transaction. After committing a transaction the autocommit mode is turned back on.- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
rollback
Rollbacks the current transaction. After rollback a transaction the autocommit mode is turned back on.- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
setSavepoint
Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it. WARNING You must turn the autocommit mode off (which is enabled by default to use Savepoints!) or start a transaction.- Returns:
- the new Savepoint object
- Throws:
MonetDBEmbeddedException- if a database access error occurs or this Connection object is currently in auto-commit mode
-
setSavepoint
Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it. WARNING You must turn the autocommit mode off (which is enabled by default to use Savepoints!) or start a transaction.- Parameters:
name- a String containing the name of the savepoint- Returns:
- the new Savepoint object
- Throws:
MonetDBEmbeddedException- if a database access error occurs or this connection object is currently in auto-commit mode
-
releaseSavepoint
Removes the given Savepoint object from the current transaction. Any reference to the savepoint after it has been removed will cause an MonetDBEmbeddedException to be thrown.- Parameters:
savepoint- the Savepoint object to be removed- Throws:
MonetDBEmbeddedException- if a database access error occurs or the given Savepoint object is not a valid savepoint in the current transaction
-
rollback
Undoes all changes made after the given Savepoint object was set. This method should be used only when auto-commit has been disabled.- Parameters:
savepoint- the Savepoint object to roll back to- Throws:
MonetDBEmbeddedException- if a database access error occurs, the Savepoint object is no longer valid, or this Connection object is currently in auto-commit mode
-
executeUpdate
Executes a SQL query without a result set, returning the number of rows it affected.- Parameters:
query- The SQL query string- Returns:
- The number of rows affected
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
executeQuery
Executes a SQL query with a result set.- Parameters:
query- The SQL query string- Returns:
- The query result object
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
prepareStatement
public MonetDBEmbeddedPreparedStatement prepareStatement(String query) throws MonetDBEmbeddedExceptionStarts a prepared statement.- Parameters:
query- The SQL query string- Returns:
- A prepared statement object where the user will set the parameters
- Throws:
MonetDBEmbeddedException- If an error in the database occurred- Since:
- 2.30
-
getMonetDBTable
public MonetDBTable getMonetDBTable(String schemaName, String tableName) throws MonetDBEmbeddedExceptionRetrieves a database table for further operations on it such as appending data.- Parameters:
schemaName- The schema of the tabletableName- The name of the table- Returns:
- A MonetDBTable instance with column details
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
getMonetDBTable
Retrieves a MonetDB Table in the current schema for further operations.- Parameters:
tableName- The name of the table- Returns:
- A MonetDBTable instance with column details
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
listTables
Performs a listing of the existing tables with schemas.- Parameters:
listSystemTables- List system's tables as well (default true)- Returns:
- The query result object,
nullif the database is not running - Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
checkIfTableExists
public boolean checkIfTableExists(String schemaName, String tableName) throws MonetDBEmbeddedExceptionCheck if a table exists in the database.- Parameters:
schemaName- The schema of the tabletableName- The name of the table- Returns:
- If a the table exists or not
- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
removeTable
Deletes a table if exists in the database.- Parameters:
schemaName- The schema of the tabletableName- The name of the table- Throws:
MonetDBEmbeddedException- If an error in the database occurred
-
closeConnectionImplementation
protected void closeConnectionImplementation()When the database shuts down, this method is called instead -
toString
A brief description of the connection. -
close
public void close()Shuts down this connection. Any pending queries connections will be immediately closed as well.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-