在Spring中设置的read-only='true' 不起作用,仍可以执行写操作;但是其他的正常,如遇到异常则事务能成功回滚等。据说是DataSourceTransactionManager不支持的原因,ibatis只能用打DataSourceTransactionManager吗?
这个问题挺郁闷,难道不能设置只读查询吗,这样会影响效率吧。
哪位知道此问题的解决办法

解决方案 »

  1.   

    是这样的,DataSourceTransactionManager不支持read-only属性的
      

  2.   

    readonly不是用来保证“读取数据的一致性”的,隔离级别是由它前面的相邻那个属性配置的 
    例如:PROPAGATION_REQUIRED,REPEATABLE READ,readOnly,-MyCheckedException,3 readOnly表示对应的事务应该被最优化为只读事务,例如在使用Hibernate时避免dirty checking 
      

  3.   

    可是我们需要的是,设置 readonly后就不能进行写操作。
    主要目的是 不要加写锁,提高读和其他线程写的速度。
      

  4.   

    我学习的过程中也遇见了同样的问题,查看相关的资料发现
    void org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(Object transaction, TransactionDefinition definition)
    里的代码
    Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
    txObject.setPreviousIsolationLevel(previousIsolationLevel); // Switch to manual commit if necessary. This is very expensive in some JDBC drivers,
    // so we don't want to do it unnecessarily (for example if we've explicitly
    // configured the connection pool to set it already).
    if (con.getAutoCommit()) {
    txObject.setMustRestoreAutoCommit(true);
    if (logger.isDebugEnabled()) {
    logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
    }
    con.setAutoCommit(false);
    }
    需要换下位置
    if (con.getAutoCommit()) {
    txObject.setMustRestoreAutoCommit(true);
    if (logger.isDebugEnabled()) {
    logger.debug("Switching JDBC Connection [" + con + "] to manual commit");
    }
    con.setAutoCommit(false);
    }

    Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
    txObject.setPreviousIsolationLevel(previousIsolationLevel);可能是ORACLE JDBC对这个顺序要限制,继续测试中
      

  5.   

    单独对ORACLE JDBC进行测试发现 autoCommit与readOlny赋值的顺序对其有影响,readonly在后则生效,readolny在前是无效的可进行insert/update/delete操作
    Class.forName("oracle.jdbc.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@xxxxx","test","test");
    con.setAutoCommit(false);
    con.setReadOnly(true);
    //con.setAutoCommit(false);
    PreparedStatement pst2 = con.prepareStatement("select * from order_tb");
    ResultSet rs = pst2.executeQuery();
    while (rs.next())
    {
    System.err.println(rs.getString(1) + "<<<<<>>>>>" + rs.getString(2)); }
    rs.close();
    pst2.close();
    String sqlStr="insert into order_tb (id,name) values (?,?)";
    sqlStr="update order_tb set id=?,name=?";
    PreparedStatement pst = con.prepareStatement(sqlStr);
    pst.setInt(1, 1);
    pst.setString(2, "ccccccc");
    pst.execute();
    pst.close();
    con.commit();