6

When reading Spring tutorials I'm quite often coming across the @Transactional annotation.

When I google it, I get documentation that says things like:

Comprehensive transaction support is among the most compelling reasons to use the Spring Framework. The Spring Framework provides a consistent abstraction for transaction management that delivers the following benefits:

or:

A transaction manager is the part of an application that is responsible for coordinating transactions across one or more resources. In the Spring framework, the transaction manager is effectively the root of the transaction system. Hence, if you want to enable transactions on a component in Spring, you typically create a transaction manager bean and pass it to the component.

I have no idea what they're referring to here.

Can someone explain?

6
  • 1
    would you mind editing the title to closer match the question? So that it would be easier for readers to see that you're asking about annotation in Spring (do I understand it correctly that your question is about it?)
    – gnat
    Commented Jul 8, 2015 at 22:07
  • @gnat - I'm not so much asking about the usage of the @Transactional annotation here, but what transactions are. Of course reference the use of @Transactional would probably help your answer.
    – dwjohnston
    Commented Jul 8, 2015 at 22:53
  • did you check Wikipedia article about transaction processing?
    – gnat
    Commented Jul 8, 2015 at 23:02
  • Recommended reading: ACID.
    – user22815
    Commented Jul 8, 2015 at 23:02
  • @gnat Yip - I found that article just as I posted the question. I think that given that the question isn't already here, it's warranted.
    – dwjohnston
    Commented Jul 8, 2015 at 23:13

2 Answers 2

13

In its broadest sense, a "transaction" is a group of actions that should be performed as if they were a single "bulk" action. The term is most often used in the context of databases, but it can be applied to many kinds of programs (particularly ones that implement a command pattern). When we're talking about databases, and often even if we aren't, we want transactions to have the so-called ACID properties:

  • Atomicity - The transaction is a single, unbreakable unit. It cannot be partially applied, or partially undone. It is either completely done, or not done at all.

  • Consistency - The application/database/whatever must be in a valid state both before and after the transaction. If attempting to execute a transaction results in an invalid state, then we must "rollback" to the last valid state.

  • Isolation - Every transaction is separate from every other transaction. No two transactions can ever "interleave". They are always executed one at a time (or in a way that is indistinguishable from "one at a time").

  • Durability - Once the transaction has been executed, it stays executed forever. This one is mostly relevant for databases, where it means the change in data has actually been committed to disk, so that the new data cannot be lost even if the machine suddenly reboots.

Not all kinds of transactions need to have all four properties, but usually when the word "transaction" is used in a programming context, that implies a system that enforces at least some if not all of them. I believe Spring is referring to database transactions and does claim to enforce all of these properties.

3
  • programmers.stackexchange.com/questions/289139/…
    – gnat
    Commented Jul 8, 2015 at 22:10
  • 1
    As far as I can tell, the "transactions" Spring is talking about are typical database transactions, so this answer should cover both readings of his question in its current form. If OP is after something else, we'll have to wait for him to edit his question.
    – Ixrec
    Commented Jul 8, 2015 at 22:12
  • ACID is valid not only for databases but transactions in other contexts as well. This includes Spring. Whether one is talking about DAOs or web transactions, the same principles apply.
    – user22815
    Commented Jul 8, 2015 at 22:59
0

A transaction is a set of actions related to each other in some way. Transaction is deemed complete when all actions have executed successfully.

One of main advantages of a transaction is ability to roll it back.

In development we have Command Pattern - commands are also transactions that can be executed, rolled back and queued.

7
  • 3
    how does this address the question about @Transactional annotation in Spring?
    – gnat
    Commented Jul 8, 2015 at 22:05
  • 2
    I don't see any question about @Transactional annotation. I was answering actual question in title: "What is a transaction?"
    – Alexus
    Commented Jul 8, 2015 at 22:08
  • "Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that – or a viable alternative..." (How to Answer)
    – gnat
    Commented Jul 8, 2015 at 22:09
  • 1
    @gnat Lol, I did, and the question states: "What is a transaction?" Then it proceeds to explain where it originates from and was was found so far.
    – Alexus
    Commented Jul 8, 2015 at 22:41
  • 1
    I think you have part of a good answer here, but it is incomplete. Given the basic nature of the question, a good answer would go into more detail on the foundational aspects of what transactions are and why they are important (as Ixrec's answer does).
    – user22815
    Commented Jul 8, 2015 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.