Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:58:39+00:00 2026-05-26T10:58:39+00:00

For a mysql connection I have a connection object and use a transaction mechanism

  • 0

For a mysql connection I have a connection object and use a transaction mechanism connection.startTransaction(), connection.commitTransaction(), connection.rollbackTransaction().

For each startTransaction(), there must always be either a call to commitTransaction() or to rollbackTransaction(). Missing such a call or calling both would break my transaction system.

So I use them in the following way:

boolean i_am_in_a_transaction=true;
try {
    connection.startTransaction();
    ...
    i_am_in_a_transaction=false;
    connection.commitTransaction();
} finally {
    if(i_am_in_a_transaction) {
        connection.rollbackTransaction();
    }
}

This ensures the declared calling order, but it is much effort, because I have to write this lines everywhere where I use transactions.

In C++ I would use a transaction object that checks in its destructor, if the commit() function was called, and that otherwise calls rollback():

class Transaction {
    public:
        Transaction()
            :am_in_transaction(false) {
        }

        ~Transaction() {
            if(_am_in_transaction) {
                rollback();
            }
        }

        void startTransaction() {
            _am_in_transaction=true;
            ...start Transaction...
        }

        void commit() {
            _am_in_transaction=false;
            ...commit Transaction...
        }

        void rollback() {
            _am_in_transaction=false;
            ...rollback Transaction...
        }

    private:
        bool _am_in_transaction;
}

This way I have the logic implemented at one place and can use it really simple:

Transaction my_transaction;
my_transaction.startTransaction;
...
my_transaction.commit();

This code is much simpler than the above java code with the try/finally block.

Is there a way to implement this behaviour in java without dedicating the logic to the caller and making him implement the try/finally block?

Something like a way to automatically call a function on scope exit would help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:58:40+00:00Added an answer on May 26, 2026 at 10:58 am

    A couple solutions come to mind…

    As @Dark Falcon points out, this would be a good use case for a try with resources call that will automatically clean up your resources at the end of the try. Unfortunately, this is only available in Java 7.

    Java classes do define a finalize() method that can get called when the object gets garbage collected, but overriding this method is almost never the right thing to do.

    I think your only other option if you’re hooked on this idea of “execute code on function return” is to using Aspect Oriented Programming. If you read up on some packages like AspectJ or look into use AOP with Spring, you can do some configuration magic to get code to execute when a function returns by intercepting the call. Here’s an example of using Spring AOP to execute other code when your function returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that globally invokes a database connection object with mySQL.
I have a MySQL table for persistence of some Java objects and use Hibernate
I use lazy connection to connect to my DB within my DB object. This
I want to select a MySQL database to use after a PHP PDO object
I have a class for each database table object. Each class takes care of
I have a pool of MySQL connections for a web-based data service. When it
I have a security consultant demanding that we implement encrypted connections to mySQL. He
I have the MySQL Connector/NET installed on my PC. I modified the source code
I am connecting a MYSQL database through hibernate and i seem to have processes
I am connecting to a MySQL DB trough a terminal who only have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.