JPA and MySQL transaction isolation level

后端 未结 3 912
北荒
北荒 2021-01-14 17:09

I have a native query that does a batch insert into a MySQL database:

    String sql = \"insert into t1 (a, b) select x, y from t2 where x = \'foo\'\";
    E         


        
相关标签:
3条回答
  • 2021-01-14 17:21

    In JPA you don't. JDO is the only standard that supports setting txn isolation. Obviously going for particular implementations methods can allow it, but then you become non-portable

    0 讨论(0)
  • 2021-01-14 17:29

    Since you are using BMT, you can do the following using a datasource to get the connection. and set the iso. level.

    DataSource source = (javax.sql.DataSource) jndiCntxt.lookup("java:comp/env/jdbc/myds");
    Connection con = source.getConnection( );
    con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    
    0 讨论(0)
  • 2021-01-14 17:42

    You need to set it at the connection level, get the session from the entitymanager and do this:

    org.hibernate.Session session = (Session)entityManager.getDelegate();
    Connection connection = session.connection();
    connection.setTransactionIsolation(Connection.READ_UNCOMMITTED);
    
    0 讨论(0)
提交回复
热议问题