Announcement Announcement Module
Collapse
No announcement yet.
Problems with Spring JDBC Transaction Management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by Surya1234 View Post
    Logically speaking the above mentioned inserts and updates should be rolled back once the exception is thrown.
    No. As far as I can tell the exception probably does nothing at all, unless you are starting a transaction somewhere before you execute that method.

    Comment


    • #17
      Next time please post code which is correct and shows us what you are trying to do.

      As dejanp mentioned your code does exactly what you tell it to do it commits after the insert and the update. The exception you throw is nice but it doesn't rollback anything because everythin is already committed.

      Code:
      insertObjectTest = (InsertObject) factory.getBean("insertObjectTEST");
      	setValueList.add(ID);
      	setValueList.add(NAME);
      	// inserting the object
              <start transaction>
      	insertObjectTest.addObject(setValueList);
              <commit transaction>
      	// ----------- END OF INSERT--------------
      
      	
      	// ----------- UPDATE operation
      
      	// preparing for updation
      	updateObjectTest = (UpdateObject) factory.getBean("updateObjectTEST");
      	setValueList = new ArrayList<String>();
      	conditionList = new ArrayList<String>();
      	setValueList.add(UPDATEDNAME);
      	conditionList.add(ID);
              <start transaction>
      	updateObjectTest.updateObject(setValueList, conditionList);
              <commit transaction>
      	throw new BusinessException();
      Also WHY oh WHY are you recreating an ApplicationContext on each method calll?! Inject your needed beans or retrieve them and don't create an new ApplicationContext each time. Code like that makes me wonder if you understand the basics of Spring and what the lifecycle of beans is. I suggest you read chapter 3 and chapter 9 of the reference guide.

      If you still have problems please post a zip file which contains the testcase/config/code so that we can see and test what is missing/going wrong in your application. You can send it to mdeinum [at] gmail [dot] com.
      Last edited by Marten Deinum; Sep 18th, 2007, 12:23 PM.

      Comment


      • #18
        Mdeinum and dejanp, thanks for the replies.
        I put the
        Code:
        new ClassPathXmlApplicationContext("testSpringJdbc.xml");
        in the example only from more readability point of view and make the function look more complete/independent. But in the actual Junit i am putting this in the setup() method. Done so to execute the various test scenarios independently. And i do know how cumbersome and heavy it would be if i were to create the factory object in each and every method.

        I even tried to debug and locate the issue, I can see the proxy getting enabled. And this proxy leads into the spring api which first sets the autocommit as off and then executes the DML statements and when the desired exception is noticed/handled it rolls back the transaction by calling the connection.rollback(). Having said this, the end outcome is that the transaction is not rolled back. And when I check in the database i can see the record.
        I may not be able to share the source files. But i can tell you, the Jdbc implementation is done using spring-jdbc's built in SQLUpdate, MappingSQLQuery, etc. The classes for jdbc implementation, which i instantiate internally extend the mentioned spring classes.

        Please note,
        The same logic works with the hibernate where the Session is kept open till the end of the transaction. And i thought the transactionManager keeps all the database connections open till the end of the transaction.
        Last edited by Surya1234; Sep 18th, 2007, 02:47 PM.

        Comment


        • #19
          How Transactions are organized depends on the TransactionManager and the implementation used.

          I still don't have the overview of your code and configuration, each example you posted doesn't give a accurate view of your situation, there is something missing each time or something you do different in the actual code.

          The combination with Hibernate/Jdbc works, very well, we use it at numerous locations in our code without any problems. It must be something in your code/configuration.

          So without a complete testcase or at least the actual configuration and problematic code I cannot help you any further.

          Comment


          • #20
            Just wanted to add a piece of information;
            the springjdbc implementation that i have works well programmatically.
            Code:
            transactionTemplate.execute(new TransactionCallback() {
            			public Object doInTransaction(TransactionStatus ts) {
            				int i =0;
            				try {
            					insertObjectTest = (InsertObject) factory
            							.getBean("insertObjectTEST");
            					setValueList.add(SpringJdbcTest.ID);
            					setValueList.add(SpringJdbcTest.NAME);
            					// inserting the object
            					insertObjectTest.addObject(setValueList);
            					setValueList.clear();
            					setValueList.add(SpringJdbcTest.ID2);
            					setValueList.add(SpringJdbcTest.NAME2);
            					// inserting the object
            					insertObjectTest.addObject(setValueList);
            
            					updateObjectTest = (UpdateObject) factory
            							.getBean("updateObjectTEST");
            					setValueList = new ArrayList<String>();
            					conditionList = new ArrayList<String>();
            					setValueList.add(SpringJdbcTest.UPDATEDNAME);
            					conditionList.add(SpringJdbcTest.ID);
            					updateObjectTest.updateObject(setValueList, conditionList);
            					setValueList.clear();
            					conditionList.clear();
            					setValueList.add(SpringJdbcTest.UPDATEDNAME2);
            					conditionList.add(SpringJdbcTest.ID2);
            					updateObjectTest.updateObject(setValueList, conditionList);
            					
            					//throwing the exception to make the transaction fail.
            					throw new RuntimeException("Transaction set to fail");
            
            				} catch (Exception ex) {
            					i=1;
            					ts.setRollbackOnly();
            				}
            				// The execution should reach here, after the exception has been thrown and caught
            				assertEquals(1, i);
            				return null;
            			}
            
            		});

            Comment


            • #21
              I give up... You don't give us the information needed to solve your problem, even after multiple request.

              Originally posted by mdeinum
              I still don't have the overview of your code and configuration, each example you posted doesn't give a accurate view of your situation, there is something missing each time or something you do different in the actual code.

              Comment


              • #22
                Originally posted by Surya1234 View Post
                The same logic works with the hibernate where the Session is kept open till the end of the transaction. And i thought the transactionManager keeps all the database connections open till the end of the transaction.
                It does, but there is no transaction defined.

                Comment


                • #23
                  Mdenium, I can understand your anger and frustration; Sorry about that. I am trying to get the needed permission for sharing the complete code.

                  Dejanp, The transaction has been defined in the xml file and i can see it get intercepted by the proxy which in turn calls the transaction advisor etc before executing the actual method.

                  Comment


                  • #24
                    If it's defined in the xml code it means that your test method is not transactional and that there is nothing to rollback. QED

                    Comment


                    • #25
                      Dejanp, i don't want my test code to be transactional. My DAO method is transactional and has been configured through the xml to be so.
                      My test method only calls this dao method which internally throws the Exception. And there are couple of DML statements to be rolled back here.

                      Comment


                      • #26
                        If you can't post the code, can't you post some code that looks like it and has the same problem?

                        Comment


                        • #27
                          Originally posted by karldmoore View Post
                          If you can't post the code, can't you post some code that looks like it and has the same problem?
                          ... usually called test cases ...

                          Joerg

                          Comment


                          • #28
                            PFA the code. Please let me know if you need more information.
                            Thanks
                            Suryanarayan

                            Comment


                            • #29
                              Source of problem ...

                              I Found the Source of the problem;
                              It has to do with the spring provided datasource; "org.springframework.jdbc.datasource.DriverManager DataSource", If i use the Atomikos provided datasource "com.atomikos.jdbc.nonxa.NonXADataSourceBean" the transactions are rolled back just the way expected. Is there a bug in DriverManagerDataSource:

                              Comment


                              • #30
                                Perhaps its the driver?

                                When you changed your datasource, did you also change your driver?
                                I am using the com.ibm.db2.jcc.DB2Driver and noticing some odd behavior with JdbcTemplate calls, specifically SP calls which appear to hang.

                                Anyway, I was googling our problem and your post came up b/c of the driver class.

                                Comment

                                Working...
                                X