Skip to content

Proposal JdbcSession

Steve Ebersole edited this page Nov 14, 2013 · 1 revision

The name here comes from the name often used on the database side. It represents a connection/transaction; a session with the user. The proposal here is to simplify Hibernate’s Connection and transaction management. The high-level view would be a class named JdbcSession that aggregates the notion of LogicalConnection with some representation of transaction.

One decision that will need to be made is whether to use the JDBC API here (Statement, PreparedStatement, ResultSet, etc) or some other representation. Initially we had tried to solve this by proxying the JDBC API using dynamic proxies, but that led to performance problems due to the dynamic invocation dispatching. Ultimately the reason to not use the JDBC object directly is to incorporate custom behavior into the JDBC interactions (e.g., consistently log statements or apply statistics). Obviously at some level we need to access the JDBC objects (Type API for binding/extracting, etc). This is more about the higher-level concepts of "Statement" and "ResultSet". A secondary purpose here is to possibly extract away the notion of JDBC at all for easier integration of non-relational back-ends (OGM, etc).

As I said initially, this JdbcSession object models a connection/transaction (maybe multiple sequential transactions). This would also be the place to hold any state with that scope such as Statement→ResultSet mappings, ongoing batches, etc.

In terms of transactions, we need both an inflow and outflow component. Inflow in that we need to be able to receive notifications of "transaction events" from the drivers of those events (org.hibernate.Transaction instances, javax.persistence.EntityTransaction instances, registered JTA sync). Outflow in that we need to be able to deal with auto-commit and Connection#commit/rollback calls.

Some proposed psuedo contracts:

interface JdbcSession implements TransactionEventInflow {
    public JdbcPhysicalTransaction makePhysicalTransaction();

    public TransactionContext getTransactionContext();
    public JdbcContext getJdbcContext();
}

// the "transaction inflow"
interface TransactionEventInflow {
    public void startingTransactionCompletion();
    public void transactionCompleted(boolean wasSucessful);
}

// the "transaction outflow", used from JdbcTransaction(Factory)
interface JdbcPhysicalTransaction {
    public void begin();
    public void commit();
    public void rollback();
}

interface JdbcContext {
    public StatementPreparer getStatementPreparer();
    pubic ResultSetExtracter getResultSetExtracter(); // currently called ResultSetReturn
    // expose some form of access to "batching"
}

interface TransactionContext {
    public TransactionImplementor getTransaction();
    public SynchronizationRegistry getSynchronizationRegistry();

    public void pulse();
}