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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:19:40+00:00 2026-05-24T21:19:40+00:00

This is a very humble question. There are some articles that indicate that one

  • 0

This is a very humble question.

There are some articles that indicate that one should always use database transactions, even for simple read operations. Here is an arbitrary example that makes a lot of sense to me: http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions The Hibernate documentation itself also says:

Always use clear transaction boundaries, even for read-only operations.

OK, seems clear enough. This has always been my assumption, viz. that since transactions are going to be applied implicitly at the database level anyway in all cases, it’s probably better to always declare them explicitly.

Then I read other articles like this: http://www.ibm.com/developerworks/java/library/j-ts1/index.html#never (note the callout). This article says, in part:

Never say never

At certain times you may want to start a transaction for a database read operation for example, when isolating your read operations for consistency or setting a specific transaction isolation level for the read operation. However, these situations are rare in business applications, and unless you’re faced with one, you should avoid starting a transaction for database read operations, as they are unnecessary and can lead to database deadlocks, poor performance, and poor throughput.

The bit about deadlocks and poor throughput also makes sense to me.

Conflicting advice at the lowest level. That’s OK; as a humble application developer, I can make up my own mind here, I think: prefer the former advice, perhaps seeing if a given ORM/database combination achieves better performance without the transaction in certain performance-critical cases. Please correct me if I’m wrong.

Now I back up into the realm of the application server and XA transactions.

If I have an EJB method that does read-only operations, is it good practice to always declare it to be transactional (following the spirit of the Hibernate advice above)? Or does marking it as @TransactionAttribute(TransactionAttributeType.SUPPORTS) not imply much about the database transaction strategy down near the metal?

What I mean is, an EJB (JTA) transaction happens at the application server level. It may be the case (I don’t know) that when a Java EE application server interacts with Hibernate that Hibernate will always enforce explicit database-level transactions, regardless of the application-server-level transaction policy in place. So the Hibernate-focused articles I’ve cited here may not apply to an application-server-level JTA transaction–perhaps it is good practice to mark read-only methods as @TransactionAttribute(TransactionAttribute.SUPPORTS) instead of REQUIRED.

What are people’s thoughts here? All pointers–even to elementary information–are welcomed.

  • 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-05-24T21:19:40+00:00Added an answer on May 24, 2026 at 9:19 pm

    If I have an EJB method that does read-only operations, is it good
    practice to always declare it to be transactional (following the
    spirit of the Hibernate advice above)?

    In my opinion it depends on the requirements of the use case you are implementing. If non repeatable reads and/or phantom reads are tolerated, your read-only method is not required to be always ran inside a transaction. For some types of searches or report generation tasks this is acceptable. On the other hand think about a situation like this:

    List<Post> posts = findAllPosts();
    for (Post p : posts) {
      Statistics s = findStatisticByPostId(p.id);
      doSomethingWithStatistics(s);
    }
    

    In such a case, it’s possible for one post to have its statistics returned by the time T1, and the other one by the time T2, where T2 > T1, as each call to findStatisticByPostId will be ran inside its own transaction. And it is possible for both of the posts to have seen some statistics during (T1...T2).

    So if you generate a report according to the returned Statistics objects, it is not possible to guarantee that the report is based on all the statistics that have been available by the time T2.

    Again depending on the requirements of what your are trying to implement, this is sometimes acceptable and sometimes not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's make this very easy. What I want: @array = qw/one two one/; my
I'm sorry for this very newbish question, I'm not much given into web development.
Important : Please see this very much related question: Return multiple values in C++
I have this very simple example that I am using to learn structs in
This one is probably very simple, but I can't seem to get it working.
I have this very simple button that I would like to hide <input type=button
I've asked about this very example in another question: Fill in a form with
I have this very simple (yet big) code that doesn't work. The triangle doesn't
This very well my be a SuperUser.com question, however it's directly related to my
This probably is a very very basic question but i can't seem to find

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.