4.3.1.20 会话

会话是集群的主要用户界面。Session 扩展了 AutoCloseable,所以它可以用在 try-with-resources 模式中。这种模式允许应用程序在 try 声明中创建一个会话,无论 try/catch/finally 块的结果如何,clusterj 都会清理并关闭会话。如果 try 块以打开的事务退出,则事务将在会话关闭之前回滚。

4.3.1.20.1 概要
 public  interface  Session extends, AutoCloseable  {
// 公共方法}  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 关闭()
public abstract void close();

指定者:close接口中 方法 AutoCloseable

关闭此会话。

4.3.1.20.3 创建查询(查询定义<T>)
public abstract Query<T> createQuery(QueryDefinition<T> qd);

从 QueryDefinition 创建查询。

表 4.45 createQuery(QueryDefinition<T>)

范围 描述
qd 查询定义
返回 查询实例

4.3.1.20.4 当前交易()
public abstract Transaction currentTransaction();

获取当前 com.mysql.clusterj.Transaction.

表 4.46 currentTransaction()

范围 描述
返回 交易

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

给定主键从数据库中删除一个类的实例。对于单列键,键参数是一个包装器(例如 Integer)。对于多列键,键参数是一个 Object[],其中元素按模式中定义的顺序对应于主键。

表 4.47 deletePersistent(Class<T>, Object)

范围 描述
控制系统 接口或动态类
钥匙 主键

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

从数据库中删除实例。只有 id 字段用于确定要删除哪个实例。如果数据库中不存在该实例,则抛出异常。

表 4.48 deletePersistent(Object)

范围 描述
实例 要删除的实例

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

从数据库中删除此类的所有实例。即使数据库中没有实例也不会抛出异常。

表 4.49 deletePersistentAll(Class<T>)

范围 描述
控制系统 接口或动态类
返回 删除的实例数

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

从数据库中删除所有参数实例。

表 4.50 deletePersistentAll(Iterable<?>)

范围 描述
实例 要删除的实例

4.3.1.20.9 查找(类<T>,对象)
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