Documentation Home
MySQL NDB Cluster API Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 3.6Mb
PDF (A4) - 3.6Mb


4.3.1.20 Session

Session is the primary user interface to the cluster. Session extends AutoCloseable so it can be used in the try-with-resources pattern. This pattern allows the application to create a session in the try declaration and regardless of the outcome of the try/catch/finally block, clusterj will clean up and close the session. If the try block exits with an open transaction, the transaction will be rolled back before the session is closed.

4.3.1.20.1 Synopsis
 public interface Session extends, AutoCloseable {
// Public Methods  public abstract void close();
  public abstract Query<T> createQuery(QueryDefinition<T> qd);
  public abstract Transaction currentTransaction();
  public abstract void deletePersistent(Class<T> cls,
                                        Object key);

  public abstract void deletePersistent(Object instance);
  public abstract int deletePersistentAll(Class<T> cls);
  public abstract void deletePersistentAll(Iterable<?> instances);
  public abstract find(Class<T> cls,
                         Object key);

  public abstract void flush();
  public abstract Boolean found(Object instance);
  public abstract QueryBuilder getQueryBuilder();
  public abstract boolean isClosed();
  public abstract load(instance);
  public abstract makePersistent(instance);
  public abstract Iterable<?> makePersistentAll(Iterable<?> instances);
  public abstract void markModified(Object instance,
                                    String fieldName);

  public abstract newInstance(Class<T> cls);
  public abstract newInstance(Class<T> cls,
                                Object key);

  public abstract void persist(Object instance);
  public abstract release(obj);
  public abstract void remove(Object instance);
  public abstract savePersistent(instance);
  public abstract Iterable<?> savePersistentAll(Iterable<?> instances);
  public abstract void setLockMode(LockMode lockmode);
  public abstract void setPartitionKey(Class<?> cls,
                                       Object key);

  public abstract String unloadSchema(Class<?> cls);
  public abstract void updatePersistent(Object instance);
  public abstract void updatePersistentAll(Iterable<?> instances);
}
4.3.1.20.2 close()
public abstract void close();

Specified by: Method close in interface AutoCloseable

Close this session.

4.3.1.20.3 createQuery(QueryDefinition<T>)
public abstract Query<T> createQuery(QueryDefinition<T> qd);

Create a Query from a QueryDefinition.

Table 4.45 createQuery(QueryDefinition<T>)

Parameter Description
qd the query definition
return the query instance

4.3.1.20.4 currentTransaction()
public abstract Transaction currentTransaction();

Get the current com.mysql.clusterj.Transaction.

Table 4.46 currentTransaction()

Parameter Description
return the transaction

4.3.1.20.5 deletePersistent(Class<T>, Object)
public abstract void deletePersistent(Class<T> cls,
                                      Object key);

Delete an instance of a class from the database given its primary key. For single-column keys, the key parameter is a wrapper (e.g. Integer). For multi-column keys, the key parameter is an Object[] in which elements correspond to the primary keys in order as defined in the schema.

Table 4.47 deletePersistent(Class<T>, Object)

Parameter Description
cls the interface or dynamic class
key the primary key

4.3.1.20.6 deletePersistent(Object)
public abstract void deletePersistent(Object instance);

Delete the instance from the database. Only the id field is used to determine which instance is to be deleted. If the instance does not exist in the database, an exception is thrown.

Table 4.48 deletePersistent(Object)

Parameter Description
instance the instance to delete

4.3.1.20.7 deletePersistentAll(Class<T>)
public abstract int deletePersistentAll(Class<T> cls);

Delete all instances of this class from the database. No exception is thrown even if there are no instances in the database.

Table 4.49 deletePersistentAll(Class<T>)

Parameter Description
cls the interface or dynamic class
return the number of instances deleted

4.3.1.20.8 deletePersistentAll(Iterable<?>)
public abstract void deletePersistentAll(Iterable<?> instances);

Delete all parameter instances from the database.

Table 4.50 deletePersistentAll(Iterable<?>)

Parameter Description
instances the instances to delete

4.3.1.20.9 find(Class<T>, Object)
public abstract find(Class<T> cls,
                       Object key);

Find a specific instance by its primary key. The key must be of the same type as the primary key defined by the table corresponding to the cls parameter. The key parameter is the wrapped version of the primitive type of the key, e.g. Integer for INT key types, Long for BIGINT key types, or String for char and varchar types. For multi-column primary keys, the key parameter is an Object[], each element of which is a component of the primary key. The elements must be in the order of declaration of the columns (not necessarily the order defined in the CONSTRAINT ... PRIMARY KEY clause) of the CREATE TABLE statement.

Table 4.51 find(Class<T>, Object)

Parameter Description
cls the interface or dynamic class to find an instance of
key the key of the instance to find
return the instance of the interface or dynamic class with the specified key

4.3.1.20.10 flush()
public abstract void flush();

Flush deferred changes to the back end. Inserts, deletes, loads, and updates are sent to the back end.

4.3.1.20.11 found(Object)
public abstract Boolean found(Object instance);

Was the row corresponding to this instance found in the database?

Table 4.52 found(Object)

Parameter Description
instance the instance corresponding to the row in the database
return
  • null if the instance is null or was created via newInstance and never loaded;

  • true if the instance was returned from a find or query or created via newInstance and successfully loaded;

  • false if the instance was created via newInstance and not found.


4.3.1.20.12 getQueryBuilder()
public abstract QueryBuilder getQueryBuilder();

Get a QueryBuilder.

Table 4.53 getQueryBuilder()

Parameter Description
return the query builder

4.3.1.20.13 isClosed()
public abstract boolean isClosed();

Is this session closed?

Table 4.54 isClosed()

Parameter Description
return true if the session is closed

4.3.1.20.14 load(T)
public abstract load(instance);

Load the instance from the database into memory. Loading is asynchronous and will be executed when an operation requiring database access is executed: find, flush, or query. The instance must have been returned from find or query; or created via session.newInstance and its primary key initialized.

Table 4.55 load(T)

Parameter Description
instance the instance to load
return the instance

4.3.1.20.15 makePersistent(T)
public abstract makePersistent(instance);

Insert the instance into the database. If the instance already exists in the database, an exception is thrown.

Table 4.56 makePersistent(T)

Parameter Description
instance the instance to insert
return the instance

4.3.1.20.16 makePersistentAll(Iterable<?>)
public abstract Iterable<?> makePersistentAll(Iterable<?> instances);

Insert the instances into the database.

Table 4.57 makePersistentAll(Iterable<?>)

Parameter Description
instances the instances to insert.
return the instances

4.3.1.20.17 markModified(Object, String)
public abstract void markModified(Object instance,
                                  String fieldName);

Mark the field in the object as modified so it is flushed.

Table 4.58 markModified(Object, String)

Parameter Description
instance the persistent instance
fieldName the field to mark as modified

4.3.1.20.18 newInstance(Class<T>)
public abstract newInstance(Class<T> cls);

Create an instance of an interface or dynamic class that maps to a table.

Table 4.59 newInstance(Class<T>)

Parameter Description
cls the interface for which to create an instance
return an instance that implements the interface

4.3.1.20.19 newInstance(Class<T>, Object)
public abstract newInstance(Class<T> cls,
                              Object key);

Create an instance of an interface or dynamic class that maps to a table and set the primary key of the new instance. The new instance can be used to create, delete, or update a record in the database.

Table 4.60 newInstance(Class<T>, Object)

Parameter Description
cls the interface for which to create an instance
return an instance that implements the interface

4.3.1.20.20 persist(Object)
public abstract void persist(Object instance);

Insert the instance into the database. This method has identical semantics to makePersistent.

Table 4.61 persist(Object)

Parameter Description
instance the instance to insert

4.3.1.20.21 release(T)
public abstract release(obj);

Release resources associated with an instance. The instance must be a domain object obtained via session.newInstance(T.class), find(T.class), or query; or Iterable, or array T[]. Resources released can include direct buffers used to hold instance data. Released resources may be returned to a pool.

Table 4.62 release(T)

Parameter Description
obj a domain object of type T, an Iterable, or array T[]
return the input parameter

Exceptions

ClusterJUserException

if the instance is not a domain object T, Iterable, or array T[], or if the object is used after calling this method.

4.3.1.20.22 remove(Object)
public abstract void remove(Object instance);

Delete the instance from the database. This method has identical semantics to deletePersistent.

Table 4.63 remove(Object)

Parameter Description
instance the instance to delete

4.3.1.20.23 savePersistent(T)
public abstract savePersistent(instance);

Save the instance in the database without checking for existence. The id field is used to determine which instance is to be saved. If the instance exists in the database it will be updated. If the instance does not exist, it will be created.

Table 4.64 savePersistent(T)

Parameter Description
instance the instance to update

4.3.1.20.24 savePersistentAll(Iterable<?>)
public abstract Iterable<?> savePersistentAll(Iterable<?> instances);

Update all parameter instances in the database.

Table 4.65 savePersistentAll(Iterable<?>)

Parameter Description
instances the instances to update

4.3.1.20.25 setLockMode(LockMode)
public abstract void setLockMode(LockMode lockmode);

Set the lock mode for read operations. This will take effect immediately and will remain in effect until this session is closed or this method is called again.

Table 4.66 setLockMode(LockMode)

Parameter Description
lockmode the LockMode

4.3.1.20.26 setPartitionKey(Class<?>, Object)
public abstract void setPartitionKey(Class<?> cls,
                                     Object key);

Set the partition key for the next transaction. The key must be of the same type as the primary key defined by the table corresponding to the cls parameter. The key parameter is the wrapped version of the primitive type of the key, e.g. Integer for INT key types, Long for BIGINT key types, or String for char and varchar types. For multi-column primary keys, the key parameter is an Object[], each element of which is a component of the primary key. The elements must be in the order of declaration of the columns (not necessarily the order defined in the CONSTRAINT ... PRIMARY KEY clause) of the CREATE TABLE statement.

Table 4.67 setPartitionKey(Class<?>, Object)

Parameter Description
key the primary key of the mapped table

Exceptions

ClusterJUserException

if a transaction is enlisted

ClusterJUserException

if a partition key is null

ClusterJUserException

if called twice in the same transaction

ClusterJUserException

if a partition key is the wrong type

4.3.1.20.27 unloadSchema(Class<?>)
public abstract String unloadSchema(Class<?> cls);

Unload the schema definition for a class. This must be done after the schema definition has changed in the database due to an alter table command. The next time the class is used the schema will be reloaded.

Table 4.68 unloadSchema(Class<?>)

Parameter Description
cls the class for which the schema is unloaded
return the name of the schema that was unloaded

4.3.1.20.28 updatePersistent(Object)
public abstract void updatePersistent(Object instance);

Update the instance in the database without necessarily retrieving it. The id field is used to determine which instance is to be updated. If the instance does not exist in the database, an exception is thrown. This method cannot be used to change the primary key.

Table 4.69 updatePersistent(Object)

Parameter Description
instance the instance to update

4.3.1.20.29 updatePersistentAll(Iterable<?>)
public abstract void updatePersistentAll(Iterable<?> instances);

Update all parameter instances in the database.

Table 4.70 updatePersistentAll(Iterable<?>)

Parameter Description
instances the instances to update