0

I have use spring jdbc in our code to store data into database.There is one scenario regarding jdbc transaction manager. I want to insert data into two table like table1 and table2. First want to insert data into table1 and after that into table2, if there is any exception occurred in between process to insert the data in table2 the whole transaction gone rollback. table1 insert and insertion logic is written in one bean and insertion logic of table 2 is into another bean how can i manage two bean below one transaction:

In my bean.xml there two entry:

<bean id="table1" class="a.b.c.database.manager.table1">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="table2" class="a.b.c.database.manager.table2">
    <property name="dataSource" ref="dataSource" />
</bean>

1 Answers1

0

Write a service, add a method with @Transactional annotation and call the table1 and table2 insertion methods from there. So all process will be transactional.. I assume you're using declarative transactions ?

Example service:

@Service
public class DataService {

  @Autowired
  Table1 table1;
  @Autowired
  Table2 table2;

  @Transactional
  public void transactionalInsert(Object data) {
    table1.insertData(data);
    table2.insertData(data);
  }
}
Gokhan Oner
  • 3,237
  • 19
  • 25