简体   繁体   English

通过oop和事务进行组合的示例

[英]Example of composition with oop and transactions

i need to insert data in two diferents tables, so i don't understand how interact the two objects for insert the data. 我需要在两个不同的表中插入数据,所以我不理解两个对象如何交互以插入数据。 i have two classes class a and class b both have two methods addA and addb the class a is composed by a classB object so in my method addA i create a new object of classB and call the addB method, IN both methods i use a connection and a preparedStatement with commit, but i need to do a rollback if the method addB doesen't work, someone could give me an example of this?, thanks! 我有两个类,类a和类b都有两个方法addA和addb,类a由classB对象组成,所以在我的方法addA中,我创建了classB的新对象并调用addB方法,在这两个方法中我都使用连接和带有commit的prepareStatement,但是如果方法addB不起作用,我需要回滚,有人可以给我一个例子吗?,谢谢!

EXAMPLE OF CLASS A
class a{

int id;
String name;
int age;

a(String name, int age){
 this.name= name;
 this.age=age;

}

public boolean addA( ) throws Exception {

    conecctions.Conn.getConnection();
    Statement stmt = null;
    Connection conection = conecctions.Conn.connection;
    Boolean result = false;


    try {
        conection.setAutoCommit(false);

        stmt = conection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String addnew= "insert into table_a values (?,?)"

         PreparedStatement prepared1= conection.prepareStatement(addnew);
         prepared1.setString(1, "name");
         prepared2.setInt(2, 25);

        prepared1.executeUpdate();

        ResultSet keys = prepared1.getGeneratedKeys();
        int lastKey = 1;
        while (keys.next()) {
          lastKey = keys.getInt(1);
        }
        this.id=lastKey;

        result = true;
        conection.commit();


        b newB= new b(this.id, this.age);
        newb.addB();

catch (Exception e2) {

        if (conection != null) {

            try {


                System.out.println("rollback");

            } catch (Exception e1) {
                e1.printStackTrace();

            }

        }
        e2.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
    }// nothing we can do
        try {
            if (conection != null)
                conection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }


    return result;


}

EXAMPLE OF CLASS B B类示例

   class b{

int id;
int idA;
int ageA;

b(String idA, int ageA){
 this.idA= idA;
 this.ageA=ageA;

}

public boolean addB( ) throws Exception {

    conecctions.Conn.getConnection();
    Statement stmt = null;
    Connection conection = conecctions.Conn.connection;
    Boolean result = false;


    try {
        conection.setAutoCommit(false);

        stmt = conection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);

        String addnew= "insert into table_b values (?,?)"

         PreparedStatement prepared2= conection.prepareStatement(addnew);
         prepared2.setInt(1, a_id);
         prepared2.setInt(2, a_age);

        prepared2.executeUpdate();



        result = true;
        conection.commit();


catch (Exception e2) {

        if (conection != null) {

            try {


                System.out.println("rollback");

            } catch (Exception e1) {
                e1.printStackTrace();

            }

        }
        e2.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
    }// nothing we can do
        try {
            if (conection != null)
                conection.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }


    return result;


}

its just a example 这只是一个例子

Simplest approach is to make 2 DAOs classes: 1 that will insert data into first table, 2nd DAO is to insert data to another table. 最简单的方法是制作2个DAO类:1个将数据插入第一个表,第二个DAO将数据插入另一个表。 Your DAO classes should extend a DAO abstract class where you initiated the connection and has the disconnect() method to disconnect the connection, resultset, preparestatement. 您的DAO类应该扩展一个DAO抽象类,在该类中您启动了连接,并具有disconnect()方法来断开连接,结果集和preparestatement。

The data that you are going to insert, where is it coming from? 您要插入的数据来自哪里?

If you want to rollback if any of these methods (addA or addB) fail, then they belong to the same transaction, so make your insert/updates inside object A and object B and leave the transaction management (try - catch, conn.setAutocommit(false), conn.commit(), conn.rollback()) to another object outside. 如果您想回滚这些方法(addA或addB)中的任何一个失败,则它们属于同一事务,因此请在对象A和对象B内进行插入/更新,并保留事务管理(try-catch,conn.setAutocommit) (false),conn.commit(),conn.rollback())到外部的另一个对象。 See also this question here and a basic documentation for jdbc transactions here . 又见这个问题, 在这里和JDBC事务一个基本的文档在这里

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

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