接口 Session
-
- 所有已知实现类:
SessionImpl
public interface SessionX DevAPI introduces a new, high-level database connection concept that is called Session. When working with X DevAPI it is important to understand this new Session concept which is different from working with traditional low-level MySQL connections.An application using the Session class can be run against a single MySQL server or large number of MySQL servers forming a sharding cluster with no code changes.
When using literal/verbatim SQL the common API patterns are mostly the same compared to using DML and CRUD operations on Tables and Collections. Two differences exist: setting the current schema and escaping names.
You cannot call
getSchema(String)orgetDefaultSchema()to obtain aSchemaobject against which you can issue verbatin SQL statements. The Schema object does not feature a sql() function.The sql() function is a method of the
Sessionclass. Usesql(String)and the SQL command USE to change the current schemaSession session = SessionFactory.getSession("root:s3kr3t@localhost");
session.sql("USE test");If a Session has been established using a data source file the name of the default schema can be obtained to change the current database.
Properties p = new Properties();
p.setProperty("dataSourceFile", "/home/app_instance50/mysqlxconfig.json");
Session session = SessionFactory.getSession(p);
String defaultSchema = session.getDefaultSchema().getName();
session.sql("USE ?").bind(defaultSchema).execute();
A quoting function exists to escape SQL names/identifiers. StringUtils.quoteIdentifier(String, boolean) will escape the identifier given in accordance to the settings of the current connection. The escape function must not be used to escape values. Use the value bind syntax of
sql(String)instead.// use bind syntax for values
session.sql("DROP TABLE IF EXISTS ?").bind(name).execute();
// use escape function to quote names/identifier
var create = "CREATE TABLE ";
create += StringUtils.quoteIdentifier(name, true);
create += "(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT");
session.sql(create).execute();Users of the CRUD API do not need to escape identifiers. This is true for working with collections and for working with relational tables.
-
-
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 voidclose()Close this session.voidcommit()Commit the transaction.SchemacreateSchema(String schemaName)Create and return a new schema with the name given by name.SchemacreateSchema(String schemaName, boolean reuseExistingObject)Create and return a new schema with the name given by name.voiddropSchema(String schemaName)Drop the existing schema with the name given by name.SchemagetDefaultSchema()Retrieve the default schema which may be configured at connect time.StringgetDefaultSchemaName()Retrieve the default schema name which may be configured at connect time.SchemagetSchema(String schemaName)Retrieve the Schema corresponding to name.List<Schema>getSchemas()Retrieve the list of Schema objects for which the current user has access.StringgetUri()Get the URL used to create this session.booleanisOpen()Is this session open?voidreleaseSavepoint(String name)Releases the named savepoint.voidrollback()Rollback the transaction.voidrollbackTo(String name)Rolls back the transaction to the named savepoint.StringsetSavepoint()Creates a transaction savepoint with an implementation-defined generated name and returns its name, which can be used inrollbackTo(String)orreleaseSavepoint(String).StringsetSavepoint(String name)Creates or replaces a transaction savepoint with the given name.SqlStatementsql(String sql)Create a native SQL command.voidstartTransaction()Start a new transaction.
-
-
-
方法详细资料
-
getSchemas
List<Schema> getSchemas()
Retrieve the list of Schema objects for which the current user has access.- 返回:
- list of Schema objects
-
getSchema
Schema getSchema(String schemaName)
Retrieve the Schema corresponding to name.- 参数:
schemaName- name of schema to retrieve- 返回:
Schema
-
getDefaultSchemaName
String getDefaultSchemaName()
Retrieve the default schema name which may be configured at connect time.- 返回:
- default schema name
-
getDefaultSchema
Schema getDefaultSchema()
Retrieve the default schema which may be configured at connect time.- 返回:
- default
Schema
-
createSchema
Schema createSchema(String schemaName)
Create and return a new schema with the name given by name.- 参数:
schemaName- name of schema to create- 返回:
Schemacreated
-
createSchema
Schema createSchema(String schemaName, boolean reuseExistingObject)
Create and return a new schema with the name given by name. If the schema already exists, a reference to it is returned.- 参数:
schemaName- name of schema to createreuseExistingObject- true to reuse- 返回:
Schemacreated
-
dropSchema
void dropSchema(String schemaName)
Drop the existing schema with the name given by name.- 参数:
schemaName- name of schema to drop
-
getUri
String getUri()
Get the URL used to create this session.- 返回:
- URI
-
isOpen
boolean isOpen()
Is this session open?- 返回:
- true if session is open
-
close
void close()
Close this session.
-
startTransaction
void startTransaction()
Start a new transaction.
-
commit
void commit()
Commit the transaction.
-
rollback
void rollback()
Rollback the transaction.
-
setSavepoint
String setSavepoint()
Creates a transaction savepoint with an implementation-defined generated name and returns its name, which can be used inrollbackTo(String)orreleaseSavepoint(String). Calling this method more than once should always work. The generated name shall be unique per session.- 返回:
- savepoint name
-
setSavepoint
String setSavepoint(String name)
Creates or replaces a transaction savepoint with the given name. Calling this method more than once should always work.- 参数:
name- savepoint name- 返回:
- savepoint name
-
rollbackTo
void rollbackTo(String name)
Rolls back the transaction to the named savepoint. This method will succeed as long as the given save point has not been already rolled back or released. Rolling back to a savepoint prior to the one named will release or rollback any that came after.- 参数:
name- savepoint name
-
releaseSavepoint
void releaseSavepoint(String name)
Releases the named savepoint. This method will succeed as long as the given save point has not been already rolled back or released. Rolling back to a savepoint prior to the one named will release or rollback any that came after.- 参数:
name- savepoint name
-
sql
SqlStatement sql(String sql)
Create a native SQL command. Placeholders are supported using the native "?" syntax.- 参数:
sql- native SQL statement- 返回:
SqlStatement
-
-