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 8216127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:01:22+00:00 2026-06-07T12:01:22+00:00

I have such things as: 1) DAO class with methods, used to perform transaction,

  • 0

I have such things as:
1) DAO class with methods, used to perform transaction, such as withdrawSum(int idAccount, float amount) and putSum(int idAccount, float amount) which use java.sql.Connection and java.sql.PreparedStatement classes to perform atomic operations with DB.
2) java.lang.reflect.InvocationHandler implementor, whick is used to get connection before transaction and commit/rollback after transaction:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Connection connection = null;
    try{
        connection = DaoUtil.INSTANCE.getConnection();
        connection.setAutoCommit(false);
        method.invoke(connection, args);
        connection.commit();
    } catch(InvocationTargetException ex){
        connection.rollback();
    } finally{
        DaoUtil.INSTANCE.closeConnection(connection);
    }
    return null;
}

3) Transaction manager, which creates Proxy instance and with its help calls method which executes transaction, something like this:

TransactionManager transManager = new TransactionManager();
InvocationHandler transHandler = new MyInvocationHandler(transManager);
TransactionManager proxy = (TransactionManager) Proxy.newProxyInstance(
            transManager.getClass().getClassLoader(), transManager.getClass().getInterfaces(), transHandler);
proxy.transferMoney(withdrawAccountid, putAccountId, transactionSum);

…..

   public void transferMoney(int withdrawAccountid, int putAccountId, float transactionSum){
            AccountDao.getInstance().withdrawSum(withdrawAccountid, transactionSum);
            AccountDao.getInstance().putSum(putAccountId, transactionSum);
    }

The question is: to execute statement in DAO methods, I need initialized Connection object. It is intialized and passed to the invoke-method of the InvocationHandler. How it should be correctly initialized in DAO-methods? Any ideas? Thanks in advance.

  • 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-06-07T12:01:23+00:00Added an answer on June 7, 2026 at 12:01 pm

    Since transactions are naturally associated with threads, a typical approach here is to store Connection in a ThreadLocal storage during the scope of transaction.

    Then you can make these connections available to DAOs using different approaches:

    • DAOs can obtain Connections by calling some static method

    • A custom DataSource can be injected into DAOs – its getConnection() method would return the connection associated with current transaction, note that connection should be proxied in order to ignore close(). This approach doesn’t couple your DAOs with transaction management code.

    Also note that all these things are already implemented by some libraries, for example, by Spring Framework. Perhaps you can leverage it instead of creating your own solution, or at least take a look at their design (in Spring, different approaches for obtaining connections are implemented by DataSourceUtils and TransactionAwareDataSourceProxy, respectively).

    See also:

    • 10. Transaction Management
    • 12. Data access with JDBC
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class StreamCopyOperation which provides me such things like the average speed
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
I have always been displaying such things in the main thread and using events
I have a Java class that has access to a transaction context that I'd
I have such things in my Java code: //...omitted some not revelant code AjaxIndicatorAppender
I swear, I have this exact thing working on another page. I'm such a
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE
i have such code (copy paste from wiki). Its multiplication of those big numbers
I have such XML structure which was returned by SharePoint web services. <rs:data ItemCount=4
i have such function to solve some logic riddle iloczyny is list with such

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.