1

i hope somebody can help me with this, i have 2 clases User and Adress both have addNewUser() and addNewAdress() this methods do a transactions with preparedsatement, but i dont know how to join the objets because when i add a new user i need to add a new adrees with the user id.

class User{
  public boolean addNewUser(){
        Connection con = conecctions.getconnection;

    Statement stmt = null;
    Boolean result = false;


            try {

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

        PreparedStatement addUser = 
           con.prepareStatement(query);
          addUser.setBoolean(1, false);
          addUser.setString(1, "name");

        con.commit();

          ...
          ...
          ...
        Adress newAdress = new Adress("new_user_id", "addres");
      newAdrees.addNewAdress();

  }

i think do that its wrong, how can i implement composition? and use the same commit for both methods. thanks!

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30
juneau
  • 11
  • 2
  • Use two separate `PreparedStatement`s with their own `INSERT INTO` SQL and only commit after both have been processed. The transaction handling should preferrably be higher up in the call stack (in the service layer if you are manually managing transactions). – Mick Mnemonic Jan 31 '18 at 21:54
  • Assuming you're using auto generated keys and your database/driver supports it, you could have a look at [`Statement#getGeneratedKeys`](https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#getGeneratedKeys--) – MadProgrammer Jan 31 '18 at 21:57
  • yes, i use two PreparedStatement for both the problem is that they are in differents objects inside addNewAdress() i use a commit too for add the new adrees in the table but i need to know if i can have the both methods separated and use one commit for both. – juneau Jan 31 '18 at 22:06

1 Answers1

1

You don't have to commit the transaction to get the insert id. (There is an answered question on how you can get it here). Then, you need to commit just once the transaction at the end of your unit of work. I would suggest to leave the transaction management outside of this object's scope, and don't commit the transaction in any of these objects (either User or Address). This will limit their scope and the code become cleaner, also the transaction code (commit-rollback-exception handling) will be standalone and reusable.

Community
  • 1
  • 1
  • Thanks so much for your answer, so in the methods addUser and addAdress you recommend to return a preparedStatement? and do the transaction outside? thanks im trying to use the oop but i dont uderstan how the methods interact – juneau Feb 06 '18 at 01:55
  • No, the Jdbc Connection interface handles the transaction and not the Jdbc PreparedStatement. So, the 'addUser' will execute the (user) update normally (st.executeUpdate()), and the 'addAddress' will execute the update in the address table too. Somewhere outside of these classes/methods you will begin ( con.setAutoCommit(false); ) and commit ( con.commit() ) the transaction. See an example here: https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html So you can do any insert/update in your database but none of them will actually persisted until and final commit is executed. – Dimitrios Kastanis Feb 06 '18 at 11:03
  • i understand that, but whe you use oop supposedly you need to create objects with their methods, so first i create the user then i create the address and both are objects they have their own methods addUser and addAdress but are different transactions, sorry if im wrong im trying to implement the transactions with oop, i'm just trying to use the best practices to do that, so in addAdress and addUser just return the sql to do the transaction outside? – juneau Feb 07 '18 at 17:19
  • In OOP every object has a small scope and must be composable. So User's addUser() inserts sql rows without knowning anything about transactionality. Another TransactionalUser object can then make it transactional WITHOUT knowing anything about sql inserts. See how can this be achieved in this code sample: https://gist.github.com/jimkast/cbe723ba605e83be43a41c9e4695509c#file-java-jdbc-transaction-example – Dimitrios Kastanis Feb 07 '18 at 19:09