Announcement Announcement Module
No announcement yet.
Queries outside a Transaction Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Queries outside a Transaction

    Hi all,

    I've recently read this article, that seems to suggest it is best to perform queries (i.e. read-only operations) outside a transaction, that is - without the @Transactional annotation - as opposed to with @Transactional(readOnly=true).

    From the article:

    This means that a transaction is started when in fact one is not required [...]. Depending on the database you are using, this can cause unnecessary shared locks, resulting in possible deadlock situations in the database. In addition, unnecessary processing time and resources are being consumed starting and stopping the transaction. The bottom line is that when you use an ORM-based framework, the read-only flag is quite useless and in most cases is ignored. But if you still insist on using it, always set the propagation mode to SUPPORTS, [...], so no transaction is started
    However, as far as I know, there is no such thing as accessing the database without a transaction.

    From the book "Java Persistence with Hibernate" (section 10.3.1):

    Many application developers think they can talk to a database outside of a transaction. This obviously isnít possible; no SQL statement can be send to a database outside of a database transaction. The term nontransactional data access means there are no explicit transaction boundaries, no system transaction, and that the behavior of data access is that of the autocommit mode. It doesnít mean no physical database transactions are involved.
    So far, I have always made sure to annotate all my data-access code as @Transactional (usually with propagation REQUIRED and with readOnly set to true for queries), but Richard's article seems to suggest otherwise.

    So, which one is it?

    BTW - I mostly use Hibernate, but IMHO, the same question applies regardless of the data-access technology used.


  • #2
    It is true: you cannot acces the database outside a transaction. However a transaction can be set as read-only, which will improve performance under certain circumstances.

    However (side note!), there are lots of simple databases out there which doesn't have transaction support. I remember the good ole' MySQL 3 or 4, which didn't have any such thing. In professional environments you should definitely use a database that supports transactions.

    IMHO, you should avoid the @Transactional annotation, since it adds extra dependencies to your code. Stick to a XML-based AOP configuration with your transaction manager, and configure all your get(..) and finder-methods as read-only.

    Best regards,


    • #3

      Let me clarify what I am asking here: the author of the article claims that performing queries outside a TX will buy me time, because certain things will not happen: database locks acquired and released, tx started and committed etc.

      But I have always understood (and the quote from the Hibernate book seems to support that understanding) - that there is no such thing as accessing the database without starting and ending a transaction! Not declaring the transaction (using Spring annotations or EJB 3.0 style for that matter) does not really make it transaction-less... the tx is still there, just configured implicitly, based on the underlying database implementation.

      Therefore, there is no such thing as "query without a transaction", hence there are no, and there cannot be, any performance gains from such a move, as the article suggests.