Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

What is meant by transaction

What is meant by Transaction ?

Could you Explain with simple word
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 6
  • 3
  • 2
  • +1
5 Solutions
 
Pramod BugudaiCommented:
Hi.

To emulate a business transaction, a program may need to perform several steps. A financial program, for example, might transfer funds from a checking account to a savings account with the steps listed in the following pseudocode:

    begin transaction
       debit checking account
       credit savings account
       update history log
    commit transaction
     

Either all three of these steps must complete, or none of them at all. Otherwise, data integrity is lost. Because the steps within a transaction are a unified whole, a transaction is often defined as an indivisible unit of work.

A transaction can end in two ways: with a commit or a rollback. When a transaction commits, the data modifications made by its statements are saved. If a statement within a transaction fails, the transaction rolls back, undoing the effects of all statements in the transaction. In the pseudocode, for example, if a disk drive crashed during the credit step, the transaction would roll back and undo the data modifications made by the debit statement. Although the transaction failed, data integrity would be intact because the accounts still balance.

In the preceding pseudocode, the begin and commit statements mark the boundaries of the transaction. When designing an enterprise bean, you determine how the boundaries are set by specifying either container-managed or bean-managed transactions.

Regards,
0
 
for_yanCommented:
Transaction is a number of databse operations which should be executed together:
think about a customer buyinh  the product - you neded to debit the account of the customer and updat
the inventory of the sold products necessarily within one trabsacation - otherwise if say second update fauils and the first succesds you'll have reall discrep[nacy in your data and many problems in your busienes
0
 
for_yanCommented:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
reijnemansCommented:
Sometime when you don't want one statment to take effect while another one is completed.

For example you want to keep track how many cars a sales manager sold and also which cars he sold. You want that both statements take affect on the same time else your data is inconsistent. The way to be sure that either both actions occur or neither action occurs is to use a transaction. A transaction is a set of one or more statements that is executed as a unit, so either all of the statements are executed, or none of the statements is executed.

So when you're commiting a transaction at the end, also statement will take effect and cancelling them will undo all statements.
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
How could I acheive transaction in java programming.

Could yougive me a piece of code?
0
 
reijnemansCommented:
 public void updateCoffeeSales(HashMap<String, Integer> salesForWeek) throws SQLException {

    PreparedStatement updateSales = null;
    PreparedStatement updateTotal = null;

    String updateString = "update " + dbName + ".COFFEES " +
                          "set SALES = ? where COF_NAME = ?";

    String updateStatement = "update " + dbName + ".COFFEES " +
                             "set TOTAL = TOTAL + ? where COF_NAME = ?";

    try {
      con.setAutoCommit(false);
      updateSales = con.prepareStatement(updateString);
      updateTotal = con.prepareStatement(updateStatement);

      for (Map.Entry<String, Integer> e : salesForWeek.entrySet()) {
        updateSales.setInt(1, e.getValue().intValue());
        updateSales.setString(2, e.getKey());
        updateSales.executeUpdate();

        updateTotal.setInt(1, e.getValue().intValue());
        updateTotal.setString(2, e.getKey());
        updateTotal.executeUpdate();
        con.commit();
      }
    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
      if (con != null) {
        try {
          System.err.print("Transaction is being rolled back");
          con.rollback();
        } catch(SQLException excep) {
          JDBCTutorialUtilities.printSQLException(excep);
        }
      }
    } finally {
      if (updateSales != null) { updateSales.close(); }
      if (updateTotal != null) { updateTotal.close(); }
      con.setAutoCommit(true);
    }
  }

Open in new window

0
 
for_yanCommented:
Usuallly JDBC works by default in the autocommit mode - so each opeation is immedaotely committed
but is you say connection.setAutocommit(false);
then you can make a number of updates and say connection.commit() - they will lall become effective simultaneously
0
 
Pramod BugudaiCommented:
0
 
for_yanCommented:
read details and code examples in this tutrorial

http://download.oracle.com/javase/tutorial/jdbc/basics/transactions.html
0
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
if anybody knows
How could i acheive the transaction in spring and hibernate
0
 
for_yanCommented:

ithis is about Hibernate and transactions
http://community.jboss.org/wiki/SessionsAndTransactions
0
 
Pramod BugudaiCommented:
This is too much. Did you followed all the comments? All your questions getting prolonged. Follow them properly and ask related questions.
0
 
for_yanCommented:

the link below is about transactions in spring, but this is indeed not that simple reading - it makes sense first to read general tutrorial about transcations - say the link from oracle above, and
then go to the more difficult part involving frameworks
http://static.springsource.org/spring/docs/2.0.8/reference/transaction.html
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now