Avoid dirty/phantom reads while selecting from database










0














I have two tables A and B.



My transactions are like this:



  • Read -> read from table A

  • Write -> write in table B, write in table A

I want to avoid dirty/phantom reads since I have multiple nodes making request to same database.



Here is an example:



  1. Transaction 1 - Update is happening on table B

  2. Transaction 2 - Read is happening on table A

  3. Transaction 1 - Update is happening on table A

  4. Transaction 2 - Completed

  5. Transaction 1 - Rollback

Now Transaction 2 client has dirty data. How should I avoid this?










share|improve this question























  • which RDBMS are you using? SQL Server, Oracle, MySQL?
    – Rich Benner
    Nov 13 '18 at 8:26










  • informix now. planning to move to SQL server
    – Raghunandan J
    Nov 13 '18 at 8:37










  • Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
    – Luís Marques
    Nov 13 '18 at 13:52










  • Dirty read is not recommended I wanted to know if there are any other solutions.
    – Raghunandan J
    Nov 13 '18 at 14:45










  • Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
    – Luís Marques
    Nov 13 '18 at 16:06















0














I have two tables A and B.



My transactions are like this:



  • Read -> read from table A

  • Write -> write in table B, write in table A

I want to avoid dirty/phantom reads since I have multiple nodes making request to same database.



Here is an example:



  1. Transaction 1 - Update is happening on table B

  2. Transaction 2 - Read is happening on table A

  3. Transaction 1 - Update is happening on table A

  4. Transaction 2 - Completed

  5. Transaction 1 - Rollback

Now Transaction 2 client has dirty data. How should I avoid this?










share|improve this question























  • which RDBMS are you using? SQL Server, Oracle, MySQL?
    – Rich Benner
    Nov 13 '18 at 8:26










  • informix now. planning to move to SQL server
    – Raghunandan J
    Nov 13 '18 at 8:37










  • Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
    – Luís Marques
    Nov 13 '18 at 13:52










  • Dirty read is not recommended I wanted to know if there are any other solutions.
    – Raghunandan J
    Nov 13 '18 at 14:45










  • Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
    – Luís Marques
    Nov 13 '18 at 16:06













0












0








0







I have two tables A and B.



My transactions are like this:



  • Read -> read from table A

  • Write -> write in table B, write in table A

I want to avoid dirty/phantom reads since I have multiple nodes making request to same database.



Here is an example:



  1. Transaction 1 - Update is happening on table B

  2. Transaction 2 - Read is happening on table A

  3. Transaction 1 - Update is happening on table A

  4. Transaction 2 - Completed

  5. Transaction 1 - Rollback

Now Transaction 2 client has dirty data. How should I avoid this?










share|improve this question















I have two tables A and B.



My transactions are like this:



  • Read -> read from table A

  • Write -> write in table B, write in table A

I want to avoid dirty/phantom reads since I have multiple nodes making request to same database.



Here is an example:



  1. Transaction 1 - Update is happening on table B

  2. Transaction 2 - Read is happening on table A

  3. Transaction 1 - Update is happening on table A

  4. Transaction 2 - Completed

  5. Transaction 1 - Rollback

Now Transaction 2 client has dirty data. How should I avoid this?







sql database transactions informix dirtyread






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 23:19









Jonathan Leffler

561k896681018




561k896681018










asked Nov 13 '18 at 8:17









Raghunandan JRaghunandan J

152214




152214











  • which RDBMS are you using? SQL Server, Oracle, MySQL?
    – Rich Benner
    Nov 13 '18 at 8:26










  • informix now. planning to move to SQL server
    – Raghunandan J
    Nov 13 '18 at 8:37










  • Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
    – Luís Marques
    Nov 13 '18 at 13:52










  • Dirty read is not recommended I wanted to know if there are any other solutions.
    – Raghunandan J
    Nov 13 '18 at 14:45










  • Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
    – Luís Marques
    Nov 13 '18 at 16:06
















  • which RDBMS are you using? SQL Server, Oracle, MySQL?
    – Rich Benner
    Nov 13 '18 at 8:26










  • informix now. planning to move to SQL server
    – Raghunandan J
    Nov 13 '18 at 8:37










  • Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
    – Luís Marques
    Nov 13 '18 at 13:52










  • Dirty read is not recommended I wanted to know if there are any other solutions.
    – Raghunandan J
    Nov 13 '18 at 14:45










  • Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
    – Luís Marques
    Nov 13 '18 at 16:06















which RDBMS are you using? SQL Server, Oracle, MySQL?
– Rich Benner
Nov 13 '18 at 8:26




which RDBMS are you using? SQL Server, Oracle, MySQL?
– Rich Benner
Nov 13 '18 at 8:26












informix now. planning to move to SQL server
– Raghunandan J
Nov 13 '18 at 8:37




informix now. planning to move to SQL server
– Raghunandan J
Nov 13 '18 at 8:37












Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
– Luís Marques
Nov 13 '18 at 13:52




Are you using DIRTY READ isolation level in Informix? In informix it is the only way to read uncommitted data.
– Luís Marques
Nov 13 '18 at 13:52












Dirty read is not recommended I wanted to know if there are any other solutions.
– Raghunandan J
Nov 13 '18 at 14:45




Dirty read is not recommended I wanted to know if there are any other solutions.
– Raghunandan J
Nov 13 '18 at 14:45












Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
– Luís Marques
Nov 13 '18 at 16:06




Informix has other isolation levels (there are restrictions depending on the type of database). Have you check the documentation? What you want is probably COMMITTED READ or COMMITTED READ LAST COMMITTED.
– Luís Marques
Nov 13 '18 at 16:06












1 Answer
1






active

oldest

votes


















1














If your database is not logged, there is nothing you can do. By choosing an unlogged database, those who set it up decided this sort of issue was not a problem. The only way to fix the problem here is change the database mode to logged, but that is not something you do casually on a whim — there are lots of ramifications to the change.



Assuming your database is logged — it doesn't matter here whether it is buffered logging or unbuffered logging or (mostly) a MODE ANSI database — then unless you set DIRTY READ isolation, you are running using at least COMMITTED READ isolation (it will be Informix's REPEATABLE READ level, standard SQL's SERIALIZABLE level, if the database is MODE ANSI).



If you want to ensure that data rows do not change after a transaction has read them, you need to run at a higher isolation — REPEATABLE READ. (See SET ISOLATION in the manual for the details. (Beware of the nomenclature for SET TRANSACTION; there's a section of the manual about Comparing SET ISOLATION and SET TRANSACTION and related sections.) The downside to using SET ISOLATION TO REPEATABLE READ (or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE) is that the extra locks needed reduce concurrency — but give you the best guarantees about the state of the database.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276604%2favoid-dirty-phantom-reads-while-selecting-from-database%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If your database is not logged, there is nothing you can do. By choosing an unlogged database, those who set it up decided this sort of issue was not a problem. The only way to fix the problem here is change the database mode to logged, but that is not something you do casually on a whim — there are lots of ramifications to the change.



    Assuming your database is logged — it doesn't matter here whether it is buffered logging or unbuffered logging or (mostly) a MODE ANSI database — then unless you set DIRTY READ isolation, you are running using at least COMMITTED READ isolation (it will be Informix's REPEATABLE READ level, standard SQL's SERIALIZABLE level, if the database is MODE ANSI).



    If you want to ensure that data rows do not change after a transaction has read them, you need to run at a higher isolation — REPEATABLE READ. (See SET ISOLATION in the manual for the details. (Beware of the nomenclature for SET TRANSACTION; there's a section of the manual about Comparing SET ISOLATION and SET TRANSACTION and related sections.) The downside to using SET ISOLATION TO REPEATABLE READ (or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE) is that the extra locks needed reduce concurrency — but give you the best guarantees about the state of the database.






    share|improve this answer

























      1














      If your database is not logged, there is nothing you can do. By choosing an unlogged database, those who set it up decided this sort of issue was not a problem. The only way to fix the problem here is change the database mode to logged, but that is not something you do casually on a whim — there are lots of ramifications to the change.



      Assuming your database is logged — it doesn't matter here whether it is buffered logging or unbuffered logging or (mostly) a MODE ANSI database — then unless you set DIRTY READ isolation, you are running using at least COMMITTED READ isolation (it will be Informix's REPEATABLE READ level, standard SQL's SERIALIZABLE level, if the database is MODE ANSI).



      If you want to ensure that data rows do not change after a transaction has read them, you need to run at a higher isolation — REPEATABLE READ. (See SET ISOLATION in the manual for the details. (Beware of the nomenclature for SET TRANSACTION; there's a section of the manual about Comparing SET ISOLATION and SET TRANSACTION and related sections.) The downside to using SET ISOLATION TO REPEATABLE READ (or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE) is that the extra locks needed reduce concurrency — but give you the best guarantees about the state of the database.






      share|improve this answer























        1












        1








        1






        If your database is not logged, there is nothing you can do. By choosing an unlogged database, those who set it up decided this sort of issue was not a problem. The only way to fix the problem here is change the database mode to logged, but that is not something you do casually on a whim — there are lots of ramifications to the change.



        Assuming your database is logged — it doesn't matter here whether it is buffered logging or unbuffered logging or (mostly) a MODE ANSI database — then unless you set DIRTY READ isolation, you are running using at least COMMITTED READ isolation (it will be Informix's REPEATABLE READ level, standard SQL's SERIALIZABLE level, if the database is MODE ANSI).



        If you want to ensure that data rows do not change after a transaction has read them, you need to run at a higher isolation — REPEATABLE READ. (See SET ISOLATION in the manual for the details. (Beware of the nomenclature for SET TRANSACTION; there's a section of the manual about Comparing SET ISOLATION and SET TRANSACTION and related sections.) The downside to using SET ISOLATION TO REPEATABLE READ (or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE) is that the extra locks needed reduce concurrency — but give you the best guarantees about the state of the database.






        share|improve this answer












        If your database is not logged, there is nothing you can do. By choosing an unlogged database, those who set it up decided this sort of issue was not a problem. The only way to fix the problem here is change the database mode to logged, but that is not something you do casually on a whim — there are lots of ramifications to the change.



        Assuming your database is logged — it doesn't matter here whether it is buffered logging or unbuffered logging or (mostly) a MODE ANSI database — then unless you set DIRTY READ isolation, you are running using at least COMMITTED READ isolation (it will be Informix's REPEATABLE READ level, standard SQL's SERIALIZABLE level, if the database is MODE ANSI).



        If you want to ensure that data rows do not change after a transaction has read them, you need to run at a higher isolation — REPEATABLE READ. (See SET ISOLATION in the manual for the details. (Beware of the nomenclature for SET TRANSACTION; there's a section of the manual about Comparing SET ISOLATION and SET TRANSACTION and related sections.) The downside to using SET ISOLATION TO REPEATABLE READ (or SET TRANSACTION ISOLATION LEVEL SERIALIZABLE) is that the extra locks needed reduce concurrency — but give you the best guarantees about the state of the database.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 1:19









        Jonathan LefflerJonathan Leffler

        561k896681018




        561k896681018



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276604%2favoid-dirty-phantom-reads-while-selecting-from-database%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党