Git Product home page Git Product logo

Comments (3)

mbraunerDE avatar mbraunerDE commented on July 26, 2024

Hey @mailtous ,

the connection.commit(); writes all data to database, only all changes until last commit can be rollbacked.

e.g.

    public void write() {
        try (Connection connection = sql2o.beginTransaction()) {
            Query q = connection.createQuery(" UPDATE `users` set name = 'alice' where id = 1");
            q.setAutoDeriveColumnNames(true);
            q.executeUpdate();
            // DO SAVE
            connection.commit();
            // TEST DATA  name is alice
            Query q = connection.createQuery(" UPDATE `users` set name = 'dirk' where id = 1");
            q.executeUpdate();
            // TEST DATA ROLLBACK, The "connection.rollback()" is executed, the name is alice, not dirk.
            connection.rollback();

        }
    }

from sql2o.

mailtous avatar mailtous commented on July 26, 2024

@mbraunerDE
Thanks your answer~! BUT...

 public void write() {
        try (Connection connection = sql2o.beginTransaction()) {
            Query q1 = connection.createQuery(" UPDATE `users` set name = 'alice' where id = 1");
            q1.setAutoDeriveColumnNames(true);
            q1.executeUpdate();
            // DO SAVE
            connection.commit();
            // TEST DATA  name is alice

            Query q2 = connection.createQuery(" UPDATE `users` set name = 'dirk' where id = 1");
            q2.executeUpdate();

            // next row need add "connection.commit()", to real save "dirk"  to DB 
           connection.commit();

            // TEST DATA ROLLBACK, The "connection.rollback()" is executed, the name is alice, not dirk.
            connection.rollback();
        }
    }

** TEST TWO: **

 public void write() {
        try (Connection connection = sql2o.beginTransaction()) {
            Query q1 = connection.createQuery(" UPDATE `users` set name = 'alice' where id = 1");
            q1.setAutoDeriveColumnNames(true);
            q1.executeUpdate();
            // DO SAVE
            connection.commit();
            // TEST DATA  name is alice

            Query q2 = connection.createQuery(" UPDATE `users` set name = 'dirk' where id = 1");
            q2.executeUpdate();

            // q2 is executed but not commit, so  name of dirk is not save to DB. 
        }
    }

from sql2o.

mbraunerDE avatar mbraunerDE commented on July 26, 2024

Hey,

you added a second commit(), so there the data will be saved and cannot be rollbacked ...

I recommend you this blog entry https://www.journaldev.com/23932/sql-commit-sql-rollback

from sql2o.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.