Announcement Announcement Module
Collapse
No announcement yet.
transactions not saved with autocommit set to false Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • transactions not saved with autocommit set to false

    Here is the relevant part of my config:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
    <property name="defaultAutoCommit"><value>false</value></property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    <property name="prepareConnection">
    <value>false</value>
    </property>
    </bean>

    <bean id="userMaintenanceManager" parent="txnProxyTemplate">
    <property name="target">
    <bean class="com.fexcodcc.dais.business.user.UserMainten anceManager">
    <property name="userDAO"> <ref bean="userDAO"/></property>
    </bean>
    </property>
    </bean>

    <bean id="userDAO"
    class="com.fexcodcc.dais.dataaccess.UserDAO">
    <property name="sessionFactory">
    <ref bean="sessi
    <bean name="txnProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">

    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    onFactory"/>
    </property>
    </bean>

    <bean name="txnProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">

    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    The problem I am having is that with defaultAutoCommit set to false my transactions are not being saved to an oracle database. I get no exception message in the logs and when I step through the code it looks like spring does actuallly commit.

    I am probably doing something stupid but I just cant see it. I have spent the last 2 days trying to figure out what Im doing wrong so any help would be greatly appreciated. Anyone have a suggesstion?

    Thanks in advance,
    T.J

  • #2
    Code:
    <property name="prepareConnection">
                 <value>false</value>
             </property>
    This setting caught my eye. Try using HB 3.0 and with this property set to true.

    Comment


    • #3
      Thanks for your suggestion Costin but my updated object does not get saved. The object I am trying to save is detached from the session that I am trying to save it in...do I need to update the persistent state of the object in the database before running the saveOrUpdate() ?

      Comment


      • #4
        No, you should not. HB re-attaches the object to the session when you are doing saveOrUpdate - if there were some problems then HB would have thrown an exception. I suggest to try another datasource (like c3p0) and work with the latest stable release - HB 3.0.5.

        Comment


        • #5
          I tried using the cp30 driver and it seems to work. However it is not clear from the documentation if auto commit is on or off by default, does anyone know? Also are there any disadvantages to using the c3p0 driver as opposed to the commons dbcp driver?

          Thanks,
          T.J

          Comment


          • #6
            I know that the HB teams recommends c3p0. You can find on HB forums several (long) threads about c3p0 versus commons-dbcp.
            See c3p0 lists and documentation - it contains a lot of parameters to see what's auto-commit default.

            Comment


            • #7
              Ok I am still having problems!

              Stepping through the code I can see that a connection is setup by Spring and auto commit is set to false. However when we reach the actual flush at the end of transaction the connections that are returned for each insert have auto commit set to true. So when the first action is inserted it is committed, then when the second action throws an exception the hibernate transaction manager tries to rollback but the first action is already committed.

              I would have thought the all connections used in the hibernate transaction manager would have auto commit set to false. How can I get the hibernate transaction manager to rollback the first insert? Is it because the getHibernateTemplate.save() does not use the connection from the transactions session but instead uses its own auto commit enabled connection?

              Thanks in advance.

              Comment


              • #8
                All connections should have the same settings - there is no distinction between the first one and the others. It should not be if you are using the same settings.
                Are you using HB 3.0.5 or HB 3.1rcX? What version of Spring are you using?

                Comment


                • #9
                  Im using HB 3.0.5 and Spring 1.2.6.

                  I would have thought all connections inside the transaction would have used the same settings as well. Maybe my transaction isnt set up properly because it doesnt seem like its behaving in a very atomic manner. Do i need to set the flush mode or something?

                  Comment


                  • #10
                    Try using a different version of Spring (1.2.4) first. If you still have this problem create a very simple example that outlines the problem and raise an issue on jira. Are you using OpenSessionInViewFilter/Interceptor?

                    Comment


                    • #11
                      Hooray..mystery finally solved!!

                      As it often turns out in these cases the solution in the end was very trivial. Our application has two application contexts, one of which is for a separate app that Im not really involved in. Its turns out that this other application context has its own hibernate transaction manager with the exact same reference name as the hibernate transaction manager in our main application context. I simply had to change the reference name of the bean in the other application context to fix the problem.

                      Thanks for all your suggestions Costin.

                      Comment


                      • #12
                        Hello

                        I am new to Hibernate and Spring and I am facing exactly same problem. My data is not commited when I set autocomit to false. I am using DBCP pool.
                        Can you share your settings ?

                        Thanks
                        Sunil

                        Comment

                        Working...
                        X