Announcement Announcement Module
Collapse
No announcement yet.
Setting of defaultAutoCommit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting of defaultAutoCommit

    Hi,
    I have a spring project and using jotm for transactionManager.
    Code:
    <bean id="dataSource"
            class="org.apache.commons.dbcp.managed.BasicManagedDataSource"
            destroy-method="close">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url"
                value="jdbc:oracle:thin:@${datasource.host}:${datasource.port}:${datasource.sid}" />
            <property name="username" value="${datasource.user}" />
            <property name="password" value="${datasource.password}" />
            <property name="defaultAutoCommit" value="false" />
            <property name="transactionManager" ref="jotm" />
        </bean>
    I'm expecting the following behavior:
    - defaultAutoCommit=true --> a commit will be issued after every single data manipulation step (e.g. SQL insert, update, etc.)
    - defaultAutoCommit=false --> a commit will be issued at the end of transaction (on exlipicite commit)

    But it doesn't work this way:

    - defaultAutoCommit=true --> data changes are comitted at the end of service
    - defaultAutoCommit=false --> NOTHING is written into database

    Currently I'm setting defaultAutoCommit to false ; so I am worry if i set it to true in case if an exception occure some of the data of a transaction will be committed and other no.

    Thanks in advancce.
    Last edited by aj1m; Jun 8th, 2011, 01:17 PM.

  • #2
    You have to configure your transaction!

    Comment


    • #3
      it is already configured, but just i didnt post it.
      and when i set the defaultAutoCommit to true the transaction is commited .

      is, what i saied about defaultAutoCommit, right?
      and do i have to use it in order to avoid the problem that i mentionned ?
      thanks

      Comment


      • #4
        You don't just have to configure your transaction managment, you have to implement where it is to be applied, a popular method now is using annotations @Transactional attribute. Lookup Transaction in the spring manual and enable annotation processing and use @Transactional annotation over methods.

        http://static.springsource.org/sprin...ansaction.html (from 10.5.6 code below)

        Code:
        <!-- enable the configuration of transactional behavior based on annotations -->
          <tx:annotation-driven transaction-manager="txManager"/>
        
          <!-- a PlatformTransactionManager is still required -->
          <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <!-- (this dependency is defined somewhere else) -->
          <property name="dataSource" ref="dataSource"/>
          </bean>
        defaultAutoCommit=true, all operations on the database are individually committed (without any need to use SQL commit commands), you do not need to configure any transaction support in Spring for stuff to get committed in SQL. even in if you do then it is possible transaction management will turn off autoCommit when you enter a transaction scope, but this can cost an extra SQL server round-trip.

        defaultAutoCommit=false (the normal mode used in JEE), you must configure transaction managment and then implement transactional behavior, if you have not correctly done both you will observe the behaviour you are seeing. SQL reads probably work ok, but all writes are left in un-committed state, when the SQL connection is handed back to the connection pool, it is possible the pooler will probably a rollback (to clear state and ensure consistent reuse next time) or it does nothing and sucessive usage of that same handle end up with more and more data in uncommitted state on the SQL server.

        Comment


        • #5
          Thank you all for your help:
          I think I'm using an older version of spring;
          so my datasource is :
          Code:
          <bean id="dataSource"
                  class="org.apache.commons.dbcp.managed.BasicManagedDataSource"
                  destroy-method="close">
                  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
                  <property name="url"
                      value="jdbc:oracle:thin:@${datasource.host}:${datasource.port}:${datasource.sid}" />
                  <property name="username" value="${datasource.user}" />
                  <property name="password" value="${datasource.password}" />
                  <property name="defaultAutoCommit" value="false" />
                  <property name="transactionManager" ref="jotm" />
              </bean>
          and jotm bean :
          Code:
          <bean id="jotmFactory"	class="com.mule.extras.jotm.JotmTransactionManagerFactory">
                  <property name="defaultTimeout" value="60"/>
              </bean>
          
          	<bean id="jotm" factory-bean="jotmFactory" factory-method="create"/>
          and the factory will return javax.transaction.TransactionManager.

          Code:
           
          private javax.transaction.TransactionManager jotmCurrent;
          public synchronized TransactionManager create() throws Exception
              {
                  if (jotmCurrent == null)
                  {
                      // check for already active JOTM instance
                      jotmCurrent = Current.getCurrent();
                      // if none found, create new local JOTM instance
                      if (jotmCurrent == null)
                      {
                          jotmCurrent = new Jotm(true, false).getTransactionManager();
                      }
                  }
                  return jotmCurrent;
              }
          Am I right in my configuration ?
          Or should I change some thing?
          By the way if i delete this line :
          Code:
          <property name="defaultAutoCommit" value="false" />
          I get a right behaviour of the application, and data are being saved in database.

          Comment


          • #6
            Does it make sense, if I delete :
            Code:
            <property name="defaultAutoCommit" value="false" />
            and does it ensures a good behaviour of my transaction.(do not commit after every single data manipulation step )

            Comment


            • #7
              You still have not showed the thread where/how the transaction management is being applied to anything.

              You are claiming to observe a behavior change when changing this setting, well this is because you are definately obtaining a JDBC connection via that bean. It is completely possible to obtain a JDBC connection from spring that is outside of the transaction management.

              I can assist with jotm, maybe it is best to stick to my example in comment #4 taken stright out of the Spring documentation. Comment-out or delete the <bean name="jotmManager" .../> until you have that example working. Place @Transactional annotation on the places where you want transactions to be working.

              It is much better to start with a working example, than to try 100 things to guess why your failing code is not working.

              One you have something working you can then try to maybe substitute your jotmManager into the configuration to replace txManager, I would also rename it to txManager when initially doing so.

              Comment


              • #8
                For persistence i am doing as the following:

                Code:
                dao.saveObject(A);
                dao.saveObject(B);
                dao.saveObject(C);
                dao.flush();
                and the methode are :
                Code:
                public void saveObject(Object o) throws DataAccessException {getHibernateTemplate().setFlushMode(HibernateAccessor.FLUSH_COMMIT);
                			getHibernateTemplate().saveOrUpdate(o);}
                Code:
                public void flush() throws DataAccessException {getHibernateTemplate().flush();}

                Comment


                • #9
                  Am I using a right configuration of my transaction?

                  Comment


                  • #10
                    Originally posted by aj1m View Post
                    For persistence i am doing as the following:

                    Code:
                    dao.saveObject(A);
                    dao.saveObject(B);
                    dao.saveObject(C);
                    dao.flush();
                    This code has to be wrapped inside transaction boundaries. This is what you have not explained in this thread.

                    Code:
                    @Service
                    public class MyServiceImpl implement MyService {
                      @Transactional
                      public void myMethod() {
                        dao.saveObject(A);
                        dao.saveObject(B);
                        dao.saveObject(C);
                        //dao.flush();   // you don't need to do this anymore
                        // just returning from the outer-most @Transactional should cause
                        // the correct flush/commit of data.  throwing an exception however
                        // should ensure a rollback.
                      }
                    }
                    Instate the MyServiceImpl via spring in a <bean ... />

                    Ensure you have setup "txManager" and transaction annotation processing. See previous link to documentation.

                    Comment

                    Working...
                    X