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

  • Home
  • SEARCH
  • 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 7993527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:52:28+00:00 2026-06-04T13:52:28+00:00

In my Java application’s DAO layer I have two DAO classes EmployeeDAO and BankDAO

  • 0

In my Java application’s DAO layer I have two DAO classes EmployeeDAO and BankDAO. I need to control/handle their database transactions. I use connection pooling to get database connections.

EmployeeDAO class:

public class EmployeeDAO {
    String name;
    String empCode;
    int age;

    // Getters & Setters
}

BankDAO class:

public class BankDAO {
    String bankName;
    String acNo;
    String empCode;

    // Getters & Setters
}

Let’s say I am going to store an Employee and Bank account details related to that employee in two database tables. First I save employee and second I save bank details and if an error occurs when storing bank details I need to rollback complete transaction.

How to manage this sort of transaction while using DAOs?

  • 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-04T13:52:30+00:00Added an answer on June 4, 2026 at 1:52 pm

    If you are using plain JDBC, what you could do is share the same instance of Connection in the two instances of the DAO classes.

    public class EmployeeDAO {
    
        private Connection conn;
    
        public void setConnection(Connection conn) {
            this.conn = conn;
        }
    
        ...
    }
    
    public class BankDAO {
    
        private Connection conn;
    
        public void setConnection(Connection conn) {
            this.conn = conn;
        }
    
        ...
    }
    

    In the client code, first you need to create a Connection object instance. Next, you need start the transaction, with conn.setAutoCommit(false);. Pass the Connection object instance to the both DAO classes. If no errors occurs in any operation, conn.commit();, otherwise, conn.rollback();

    e.g.:

    Connection conn = null;
    try {
        // getConnection from pool
    
        conn.setAutoCommit(false);
    
        EmployeeDAO employeeDAO = new EmployeeDAO();
        employeeDAO.setConnection(conn);
    
        BankDAO bankDAO = new BankDAO();
        bankDAO.setConnection(conn);
    
        // save employee
    
        // save bank details
    
        conn.commit();
    
    catch(Exception e) {
        if (conn != null) {
            conn.rollback();
        }
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java-application using JDBC for database interaction. I want to do a
Our Java application has about 100 classes mapped to a database (SQL Server or
I have a java application in which i need to run the commands as
I have a java application that creates two static objects in a base class,
In my Java application i need to compare two list's element whether it is
We have a Java application under Mercurial version control. We're working on implementing a
In my Java application, I need to connect to the same host using SSL,
i'm developing a java application using the jstun library (hxxp://jstun.javawi.de/), and i need to
I tried decompiling a Java application to which I do not have the source
I have a java application, and I want to use rails migrations for this.

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.