JDBC DataContext

The JDBC DataContext implementation, JdbcDataContext, provides access to relational JDBC databases via MetaModel.

The class features:

Instantiation via factory/properties

Here is an example properties set, useable to instantiate a JDBC DataContext:

type=jdbc
url=jdbc:mysql://localhost/sakila?defaultFetchSize=-2147483648
driver-class=com.mysql.jdbc.Driver
username=john
password=doe
catalog=
table-types=

Simply load these properties and pass them to MetaModel's DataContextFactoryRegistryImpl class:

Properties props = new Properties();
props.load(new FileInputStream("my_connection.properties");

DataContext dataContext = DataContextFactoryRegistryImpl.getDefaultInstance().createDataContext(props);

Database compliancy

See DatabaseCompliancy for a list of JDBC databases that are known to work with MetaModel via JdbcDataContext.

Transactionality and batch updates

The JdbcDataContext class also implements the UpdateableDataContext interface, which enables it to perform updates on the database. There are two distinct modes in which updates can happen in a JDBC context:

Transactional update

If a regular/default UpdateScript is passed into the executeUpdate method, then it means that the entirety of the update is performed using the ACID properties that the database provides:

dataContext.executeUpdate(new UpdateScript() {
  public void run(UpdateCallback callback) {
    // invoke transactional updates using 'callback' here
  }
}),

The boundaries of the transaction is the same as the boundaries of the run(UpdateCallback) method call.

Batch update

JDBC also supports batch updates which typically perform better when the amount of changes are greater. In MetaModel, batch updates are enabled simply when the BatchUpdateCallback class is used instead of the regular UpdateCallback class:

dataContext.executeUpdate(new UpdateScript() {
  public void run(BatchUpdateCallback callback) {
    // invoke batch updates using 'callback' here
  }
}),

Most JDBC implementations retain the transactional behaviour also for batch updates, but some implementations do have side effects such as non-atomic updates or dirty reads. Check your database vendor's JDBC driver documentation for details.


CategoryExamples

examples/JdbcDataContext (last edited 2016-08-08 03:56:29 by KasperSorensen)