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 Session#getSchema(String) or Session#getDefaultSchema() to obtain a Schema object 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 Session class. Use Session#sql(String) and the SQL command USE to change the current
schema
Session 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 Session#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.
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public Schema | Returns: Schema createdname of schema to create schemaName)Create and return a new schema with the name given by name. |
| public Schema | Returns: Schema createdname of schema to create schemaName, boolean true to reuse reuseExistingObject)Create and return a new schema with the name given by name. |
| public void | dropSchema(String
name of schema to drop schemaName)Drop the existing schema with the name given by name. |
| public Schema | getDefaultSchema()
Retrieve the default schema name, which might have been configured at the time of connection. |
| public String | Returns: default schema name or empty String if no default schema has been setRetrieve the default schema name, which might have been configured at the time of connection. |
| public Schema | |
| public List | Returns: list of Schema objectsRetrieve the list of Schema objects for which the current user has access. |
| public String | |
| public boolean | |
| public void | |
| public void | |
| public void | |
| public String | Returns: savepoint nameCreates a transaction savepoint with an implementation-defined generated name and returns its name, which can be used in |
| public String | Returns: savepoint namesavepoint name name)Creates or replaces a transaction savepoint with the given name. |
| public SqlStatement | |
| public void |
| close | back to summary |
|---|---|
| public void close() Redeclares java. Close this session.
| |
| commit | back to summary |
|---|---|
| public void commit() Commit the transaction. | |
| createSchema | back to summary |
|---|---|
| public Schema createSchema(String schemaName) Create and return a new schema with the name given by name. | |
| createSchema | back to summary |
|---|---|
| public 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. | |
| dropSchema | back to summary |
|---|---|
| public void dropSchema(String schemaName) Drop the existing schema with the name given by name.
| |
| getDefaultSchema | back to summary |
|---|---|
| public Schema getDefaultSchema() Retrieve the default schema name, which might have been configured at the time of connection. Returns | |
| getDefaultSchemaName | back to summary |
|---|---|
| public String getDefaultSchemaName() Retrieve the default schema name, which might have been configured at the time of connection. Returns empty String if no default schema has been set.
| |
| getSchema | back to summary |
|---|---|
| public Schema getSchema(String schemaName) Retrieve the Schema corresponding to name. | |
| getSchemas | back to summary |
|---|---|
| public List Retrieve the list of Schema objects for which the current user has access. | |
| getUri | back to summary |
|---|---|
| public String getUri() Get the URL used to create this session.
| |
| isOpen | back to summary |
|---|---|
| public boolean isOpen() Is this session open?
| |
| releaseSavepoint | back to summary |
|---|---|
| public 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.
| |
| rollback | back to summary |
|---|---|
| public void rollback() Rollback the transaction. | |
| rollbackTo | back to summary |
|---|---|
| public 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.
| |
| setSavepoint | back to summary |
|---|---|
| public String setSavepoint() Creates a transaction savepoint with an implementation-defined generated name and returns its name, which can be used in
| |
| setSavepoint | back to summary |
|---|---|
| public String setSavepoint(String name) Creates or replaces a transaction savepoint with the given name. Calling this method more than once should always work. | |
| sql | back to summary |
|---|---|
| public SqlStatement sql(String sql) Create a native SQL command. Placeholders are supported using the native "?" syntax.
| |
| startTransaction | back to summary |
|---|---|
| public void startTransaction() Start a new transaction. | |