You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by I L <is...@hotmail.com> on 2009/04/08 03:19:31 UTC

Using Connection.commit() in place of SqlMapSession.commitTransaction(). "commitRequired" Benefit

Hi guys,

This is a two part question.

1) Is there a significant performance pickup if you use commitRequired="false". As you know this attribute is set to false by default. I couldn't find any information on the benefit of keeping that field false.

2) I would like to run an sql update query using a Statement object. Its for throw away install scripts that I do not want the sql to be in the xml files.

So if I run an sql update query using a Statement object with commitRequired="false", the expected behavior of sqlMapSession.commitTransaction() would be to do nothing because ibatis didn't detect the update. So what I ended up doing is the following code snippet:

--------
  sqlMapSession.startTransaction();

  Connection connection = sqlMapSession.getCurrentConnection();
  Statement stmt = connection.createStatement()

  stmt.executeUpdate("UPDATE item SET type="NA");
            
  connection.commit(); //did not use sqlMapSession.commitTransaction()
-------

Note how I used the connection's commit() instead of commitTransaction(). Is this ok? Am I bypassing something that could be dangerous? From my testing, it shows that everything is ok.

Thank you in advance.

Isster


_________________________________________________________________
Rediscover HotmailĀ®: Get quick friend updates right in your inbox. 
http://windowslive.com/RediscoverHotmail?ocid=TXT_TAGLM_WL_HM_Rediscover_Updates1_042009

RE: Using Connection.commit() in place of SqlMapSession.commitTransaction(). "commitRequired" Benefit

Posted by I L <is...@hotmail.com>.






To be safe, I created a commit wrapper that goes out and sets the forceCommit flag to true, then calls commitTransaction(). I didn't feel comfortable about calling connection.commit() directly. This allows me to selectively choose when I want to force a commit or have ibatis check the isCommitRequired flag. My only worry left is if an instance of TransactionConfig shared by other transactions and if so, will  changing its value effect the other transactions as well? Or is TransactionConfig cloned for each transaction. I wouldn't mind if the other transactions temporarily forced a commit while this one finished up but I am just curious.

    public void forceCommitWork(){
        try{
            if(sqlMapSession instanceof SqlMapSessionImpl){
            
                SqlMapExecutorDelegate sqlMapExecutorDelegate = ((SqlMapSessionImpl) sqlMapSession).getDelegate();
                TransactionManager transManager = sqlMapExecutorDelegate.getTxManager();
                
                TransactionConfig config = transManager.getConfig();
                
                //remember the original force commit setting
                boolean originalForceCommitSetting = config.isForceCommit();
                
                //temporarly change it to true
                config.setForceCommit(true);
                
                sqlMapSession.commitTransaction();
                
                //revert the force commit setting
                config.setForceCommit(originalForceCommitSetting);
            }else{
                throw new TechnicalException("Expected sqlMapSession to be an instanceof SqlMapSessionImpl");
            }
        }catch(SQLException se){
            throw new TechnicalException(se.getMessage());
        }
    }



From: isster@hotmail.com
To: user-java@ibatis.apache.org
Subject: Using Connection.commit() in place of SqlMapSession.commitTransaction(). "commitRequired" Benefit
Date: Wed, 8 Apr 2009 01:19:31 +0000








Hi guys,

This is a two part question.

1) Is there a significant performance pickup if you use commitRequired="false". As you know this attribute is set to false by default. I couldn't find any information on the benefit of keeping that field false.

2) I would like to run an sql update query using a Statement object. Its for throw away install scripts that I do not want the sql to be in the xml files.

So if I run an sql update query using a Statement object with commitRequired="false", the expected behavior of sqlMapSession.commitTransaction() would be to do nothing because ibatis didn't detect the update. So what I ended up doing is the following code snippet:

--------
  sqlMapSession.startTransaction();

  Connection connection = sqlMapSession.getCurrentConnection();
  Statement stmt = connection.createStatement()

  stmt.executeUpdate("UPDATE item SET type="NA");
            
  connection.commit(); //did not use sqlMapSession.commitTransaction()
-------

Note how I used the connection's commit() instead of commitTransaction(). Is this ok? Am I bypassing something that could be dangerous? From my testing, it shows that everything is ok.

Thank you in advance.

Isster


_________________________________________________________________
Rediscover HotmailĀ®: Get quick friend updates right in your inbox. 
http://windowslive.com/RediscoverHotmail?ocid=TXT_TAGLM_WL_HM_Rediscover_Updates1_042009