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

Posted on 2009-05-14
Medium Priority
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.
Question by:VikasMahajan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 12

Accepted Solution

Gibu George earned 2000 total points
ID: 24393957
For transaction management use the declarative transaction management which will affect the code leaset. To understand transaction manaagement

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);
        	    for (int i = 0; i < mt940.getLineCount(); i++) {
                	if (logger.isDebugEnabled()) {
            	        logger.debug("sql is : " + mt940.toLineInsertSQL(i,key));

Open in new window

LVL 12

Assisted Solution

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

Expert Comment

by:Gibu George
ID: 24394023
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


Author Comment

ID: 24396563
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.
LVL 12

Assisted Solution

by:Gibu George
Gibu George earned 2000 total points
ID: 24396706
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.

Expert Comment

ID: 26406123
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.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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