简体   繁体   English

使用DAO Spring MVC将行插入到多个表中

[英]Inserting rows into multiple tables using DAOs Spring MVC

I have Registration page in which I am inserting data into multiple tables 1. User Details 2. Security Details 3. User Reference Details 4. User Access details 我有“注册”页面,在其中将数据插入多个表中。1.用户详细信息2.安全性详细信息3.用户参考详细信息4.用户访问详细信息

For each table, I am using I am using single DAO and in turn each DAO impl has corresponding insert statement. 对于每个表,我正在使用单个DAO,每个DAO impl又具有相应的insert语句。

Now, it became difficult to handle and rollback insertions. 现在,变得难以处理和回滚插入。 Can we handle all insert statements in Single DAO rather than using multiple DAO. 我们能否在单个DAO中而不是使用多个DAO中处理所有插入语句。 What is the best practice 最佳做法是什么

I don't want to use hibernate or related .. 我不想使用休眠或相关..

If handling transaction of all the inserts in one shot is your primary concern, then you can refer to the following: 如果您主要关注一次处理所有插入件的事务,则可以参考以下内容:

    Connection connection;
    try
    {
        // Instantiate connection using DriverManager
        ...
        connection.setAutoCommit(false);
        ...
        // Inserts go here
        ...
        connection.commit();
    }catch(Exception e){
        connection.rollback(); // rollback in case of exceptions
    }finally{
        // release databases resources
    }

A commit is performed only if all the inserts are successful, otherwise the transaction is rolled back. 仅当所有插入都成功时才执行提交,否则事务将回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM