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 long
connectionPointer
The C connection pointer. -
Constructor Summary
Constructors Modifier Constructor Description protected
MonetDBEmbeddedConnection(long connectionPointer)
-
Method Summary
Modifier and Type Method Description boolean
checkIfTableExists(String schemaName, String tableName)
Check if a table exists in the database.void
close()
Shuts down this connection.protected void
closeConnectionImplementation()
When the database shuts down, this method is called insteadvoid
commit()
Commits the current transaction.QueryResultSet
executeQuery(String query)
Executes a SQL query with a result set.int
executeUpdate(String query)
Executes a SQL query without a result set, returning the number of rows it affected.boolean
getAutoCommit()
Retrieves the current auto-commit mode for this connection.MonetDBTable
getMonetDBTable(String tableName)
Retrieves a MonetDB Table in the current schema for further operations.MonetDBTable
getMonetDBTable(String schemaName, String tableName)
Retrieves a database table for further operations on it such as appending data.long
getRandomIdentifier()
Retrieves a long number randomly generated, used to identify the connection uniquely.String
getSchema()
Retrieves the current schema set on the connection.boolean
isClosed()
Tells if the connection has been closed or not.QueryResultSet
listTables(boolean listSystemTables)
Performs a listing of the existing tables with schemas.MonetDBEmbeddedPreparedStatement
prepareStatement(String query)
Starts a prepared statement.void
releaseSavepoint(Savepoint savepoint)
Removes the given Savepoint object from the current transaction.void
removeTable(String schemaName, String tableName)
Deletes a table if exists in the database.void
rollback()
Rollbacks the current transaction.void
rollback(Savepoint savepoint)
Undoes all changes made after the given Savepoint object was set.void
setAutoCommit(boolean autoCommit)
Sets this connection's auto-commit mode to the given state.Savepoint
setSavepoint()
Creates an unnamed savepoint in the current transaction and returns the new Savepoint object that represents it.Savepoint
setSavepoint(String name)
Creates a savepoint with the given name in the current transaction and returns the new Savepoint object that represents it.void
setSchema(String newSchema)
Sets the current schema for this connection.void
startTransaction()
Begins a transaction.String
toString()
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,
null
if 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-