Solved

Is there a example of using Spring JDBCTemplate to use batch insert with transaction around it

Posted on 2009-05-14
6
8,296 Views
Last Modified: 2013-11-13
Is there a example of using Spring JDBCTemplate to use batch insert with transaction around it. I have two tables one parent table and other child table. The parent and child tables have 1 to many relationship. I want to do batch insert for parent table and at the same time that should also do batch insert in child tables with the Foreign Key generated from parent table. I want to enclose that insert in transaction so that if anything in child table fails I can roll back that particular insert from parent.
0
Comment
Question by:VikasMahajan
  • 4
6 Comments
 
LVL 12

Accepted Solution

by:
Gibu George earned 500 total points
Comment Utility
For transaction management use the declarative transaction management which will affect the code leaset. To understand transaction manaagement
http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html

The sample code to do the transaction management is like this(not declarative)
public void saveMt940(final Mt940 mt940) throws DataAccessException {

	    TransactionTemplate tt = new TransactionTemplate();

	    tt.setTransactionManager(new DataSourceTransactionManager(getDataSource()));

	    tt.execute(new TransactionCallbackWithoutResult() {

            protected void doInTransactionWithoutResult(TransactionStatus status) {

                OracleSequenceMaxValueIncrementer inc = new OracleSequenceMaxValueIncrementer(getDataSource(),"TRANSAKCIJE_940_SEQ");

        	    JdbcTemplate jt = getJdbcTemplate();

        	    int key = inc.nextIntValue();

        	    if (logger.isDebugEnabled()) {

        	        logger.debug("key is : " + key);

        	    }

        	    jt.update(mt940.toHeadInsertSQL(key));

        	    for (int i = 0; i < mt940.getLineCount(); i++) {

                	if (logger.isDebugEnabled()) {

            	        logger.debug("sql is : " + mt940.toLineInsertSQL(i,key));

            	    }

                	jt.update(mt940.toLineInsertSQL(i,key));

                }

            }

	    });

	}

Open in new window

0
 
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 500 total points
Comment Utility
You can perform batch inserts via batchUpdate() as well. Also there are convenient overloaded executeBatch() method at SimpleJdbcInsert
0
 
LVL 12

Expert Comment

by:Gibu George
Comment Utility
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:VikasMahajan
Comment Utility
Thanks gibu!!
Yes, I was thinking of using Spring transaction management. We do not yet have that setup in application so first I will have to do that. At this point I am not sure what kind of transaction (declarative or programable) will be suitable for my application. We have services and those services call the DAO and I was thinking of taking care of transaction in DAO due to the batch inserts and dependencies of the parent and child table. What would you recommend.
0
 
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 500 total points
Comment Utility
The declarative one is a lot cleaner. If you are doing the transaction management by writing code it should be done at the DAO layer, not at the Service layer.
0
 
LVL 1

Expert Comment

by:sheepfest
Comment Utility
IMHO, translactions should be managed at the Service layer, not at the DAO. What about services that require multiple DAO objects to be used within the same transaction? Far easier and cleaner to wrap the service layer method in a transaction.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now