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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:37:11+00:00 2026-05-23T14:37:11+00:00

The following code fails to create a new record reliably. TransferRecord transfer = new

  • 0

The following code fails to create a new record reliably.

TransferRecord transfer = new TransferRecord();
transfer.setTransferId(metaData.getTransferId());
transfer.setUserName(metaData.getUserId().getUserName());
transfer.setCancelled(false);
em.merge(transfer);
em.flush();

Here’s the code for the transfer record:

/***
 * Contains a record of an ongoing transfer.
 * @author anchapma
 *
 */
@Entity
@Table(name = "TransferRecord")
public class TransferRecord implements Serializable
{
    /**
     * Generated static unique ID.
     */
    private static final long serialVersionUID = -8131586518447878482L;

    /***
     * The id of the transfer.
     */
    private String transferId;

    /***
     * The name of the user who ownes the transfer.
     */
    private String username;

    /***
     * Has this transfer been cancelled?
     */
    private boolean cancelled;

    /***
     * Constructor.
     */
    public TransferRecord()
    {
        this.transferId = "";
        this.username = "";
        this.cancelled = false;
    }

    /***
     * Gets the transfer ID.
     * 
     * @return The transfer ID.
     */
    @Id
    @Column(name = "TransferID", unique = true, nullable = false, length = 50)
    public String getTransferId()
    {
        return this.transferId;
    }

    /***
     * Sets the transfer ID.
     * @param transferId The new transfer ID.
     */
    public void setTransferId(String transferId)
    {
        this.transferId = transferId;
    }

    /***
     * Gets the username.
     * 
     * @return The username.
     */
    @Column(name = "UserName", nullable = false, length = 50)
    public String getUserName()
    {
        return this.username;
    }

    /***
     * Sets the username.
     * @param username The new username.
     */
    public void setUserName(String username)
    {
        this.username = username;
    }

    /***
     * Gets whether or not the transfer has been cancelled.
     * 
     * @return True if the transfer has been cancelled.
     */
    @Column(name = "Cancelled", nullable = false, length = 50)
    public boolean getCancelled()
    {
        return this.cancelled;
    }

    /***
     * Sets whether or not the transfer has been cancelled.
     * @param cancelled True if the transfer has been cancelled.
     */
    public void setCancelled(boolean cancelled)
    {
        this.cancelled = cancelled;
    }
}

I think that what is happening is the record gets added to the database after a delay. My code uses the existence or absence of a TransferRecord record as a flag. It therefore needs the data to show up in the table immediately.

Am I likely to be right in my assumption? If so, is there a way to force the em.flush() call to wait until it has written out the record before returning?

  • 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-23T14:37:12+00:00Added an answer on May 23, 2026 at 2:37 pm

    The accompanying comment to the question states –

    “The calling bean is stateless and executes for many seconds. The data doesn’t show up to other database users until the bean’s operation ends.”

    This described behavior has nothing to do with flushing the persistence context associated with the EntityManager. It has a lot to do with the transaction isolation level associated with the transaction. The transaction associated with the stateless session bean (SLSB) actually commits data to the database, only on returning from the bean method, as each SLSB method might be associated with the default transaction attribute of REQUIRED (which uses an existing transaction or starts a new one); the resulting behavior is that the transaction is terminated via a rollback in the SLSB method or via a commit on return from the method.

    This behavior affects reads performed by other clients and transactions since the resulting outcome depends on the isolation level of the current pertinent transaction, which in turn depends on the isolation level specified on the database connection pool. For most databases and associated connection pools, the transaction isolation level happens to be READ COMMITTED, and therefore other transactions can read only data committed by the pertinent transaction (i.e. on SLSB method return). This is desirable behavior in most circumstances, for you do not wish to read data that is uncommitted (and may later be rolled back) resulting in the possibility of dirty reads.

    If you do intend to perform dirty reads, you must configure the JDBC connection pool to allow dirty reads, or in other words, set the transaction isolation level of the pool to READ UNCOMMITTED. Configuration changes would vary from container to container, and would also be subject to support of the READ UNCOMMITTED isolation level by the database; Oracle for instance, does not allow you to set the isolation level to READ UNCOMMITTED, and may set it to the next higher isolation level supported by it (READ COMMITTED).

    If you want to avoid mucking around with transaction isolation levels, consider splitting the method calls across several transactions. You can achieve this by creating a new business method in separate SLSB that requires a new transaction to be created on every invocation (REQUIRES_NEW). The Pseudo-code example is as outlined below:

    @Stateless
    @Local(X.class)
    public class SLSBX implements X {
    
        @EJB Y y;
    
        @TransactionAttribute(TransactionAttributeType.REQUIRED) // start a new transaction to do work
        public void sampleMethod() {
             // suspends the existing transaction on invoking Y.anotherSampleMethod()
             Y.anotherSampleMethod();
             // continue doing work in the current transaction. Work done in anotherSampleMethod() would have either committed or rolled back.
        }
    }
    
    @Stateless
    @Local(Y.class)
    public class SLSBY implements Y {
        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // Suspends the existing transaction and creates a new transaction that terminates on method return
        public void anotherSampleMethod() {
            // do some stuff
        }
    }
    

    This approach is of course, recommended only if the business nature of the transaction allows for adoption of such an approach. Typically, one must scope all business activities that are part of a business transaction, into an actual transaction.

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

Sidebar

Related Questions

The following code fails at runtime… Dim Id As Guid = CType(e.CommandArgument, Guid) It
The following code fails to compile stating A local variable named 'st' cannot be
For some reason the following code fails. You can't simply erase a reverse_iterator by
I'm trying to use opengl in C#. I have following code which fails with
The following html code works in Firefox, but for some reason fails in IE
The following code is good for uploading text files, but it fails to upload
I have a datatable AVT of type AvailDataTable. The following code works to create
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.