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.
Author:
Pedro Ferreira
  • Field Details

    • connectionPointer

      protected volatile long connectionPointer
      The C connection pointer.
  • Constructor Details

  • 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

      public String getSchema() throws MonetDBEmbeddedException
      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

      public void setSchema​(String newSchema) throws MonetDBEmbeddedException
      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

      public boolean getAutoCommit() throws MonetDBEmbeddedException
      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

      public void setAutoCommit​(boolean autoCommit) throws MonetDBEmbeddedException
      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

      public void startTransaction() throws MonetDBEmbeddedException
      Begins a transaction. WARNING Starting a transaction will turn the autocommit mode off!
      Throws:
      MonetDBEmbeddedException - If an error in the database occurred
    • commit

      public void commit() throws MonetDBEmbeddedException
      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

      public void rollback() throws MonetDBEmbeddedException
      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

      public Savepoint setSavepoint() throws MonetDBEmbeddedException
      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

      public Savepoint setSavepoint​(String name) throws MonetDBEmbeddedException
      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

      public void releaseSavepoint​(Savepoint savepoint) throws MonetDBEmbeddedException
      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

      public void rollback​(Savepoint savepoint) throws MonetDBEmbeddedException
      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

      public int executeUpdate​(String query) throws MonetDBEmbeddedException
      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

      public QueryResultSet executeQuery​(String query) throws MonetDBEmbeddedException
      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 MonetDBEmbeddedException
      Starts 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 MonetDBEmbeddedException
      Retrieves a database table for further operations on it such as appending data.
      Parameters:
      schemaName - The schema of the table
      tableName - The name of the table
      Returns:
      A MonetDBTable instance with column details
      Throws:
      MonetDBEmbeddedException - If an error in the database occurred
    • getMonetDBTable

      public MonetDBTable getMonetDBTable​(String tableName) throws MonetDBEmbeddedException
      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

      public QueryResultSet listTables​(boolean listSystemTables) throws MonetDBEmbeddedException
      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 MonetDBEmbeddedException
      Check if a table exists in the database.
      Parameters:
      schemaName - The schema of the table
      tableName - The name of the table
      Returns:
      If a the table exists or not
      Throws:
      MonetDBEmbeddedException - If an error in the database occurred
    • removeTable

      public void removeTable​(String schemaName, String tableName) throws MonetDBEmbeddedException
      Deletes a table if exists in the database.
      Parameters:
      schemaName - The schema of the table
      tableName - 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

      public String toString()
      A brief description of the connection.
      Overrides:
      toString in class Object
    • close

      public void close()
      Shuts down this connection. Any pending queries connections will be immediately closed as well.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable