0

in a project I need to set the transaction isolation level to SNAPSHOT, I know that it is possible to set it in java with

System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);

// STEP 4: Execute a query
System.out.println("Creating statement...");
conn.setTransactionIsolation(4096);
conn.setAutoCommit(false);

unfortunately I didn't find this possibility in Mybatis so is there a way to pass Mybatis a jdbc connection on which I already have setted it to this isolation level?

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58

2 Answers2

1

It depends on your needs, but you could for example:

  • set the default transaction isolation level directly in the database and don't bother with the setting in Java;
  • you could create yourself a DataSource that before returning a connection sets the desired isolation level on it. You can then configure it in the environment instead of those MyBatis provides;
  • in some circumstances MyBatis allows you to specify a connection or the isolation level when opening a session. You might want to read the documentation to see what your options are.
Bogdan
  • 23,890
  • 3
  • 69
  • 61
0

You might extend JdbcTransactionFactory as well JdbcTransaction class and pass any parameters you like as stated in documentation:

could put your own fully qualified class name or Type Alias that refers to your own implementation of the TransactionFactory interface

Any properties configured in the XML will be passed to the setProperties() method after instantiation

<transactionManager type="com.example.CustomTansactionFactory">
  <property name="isolationLevel" value="4096"/>
</transactionManager>

Using these two interfaces, you can completely customize how MyBatis deals with Transactions.

public class CustomTansactionFactory extends JdbcTransactionFactory {
  ...
  public Transaction newTransaction(Connection conn, boolean autoCommit) {
    return new CustomJdbcTransaction(conn, autoCommit, isolationLevel);
  }
  ...
}