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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:48:50+00:00 2026-05-11T17:48:50+00:00

I have a MDB (Message driven bean) that receives messages with String which represent

  • 0

I have a MDB (Message driven bean) that receives messages with String which represent a word. Also I have a table in the database. The MDB should store in the table, the words and the number of times each word was received (counter).

The problem is that to get better performance the MDB started in many instances and when the same new word is received by different instances they both create the same row with count of 1.

To solve this I should make the word field unique and then the second instance will fail on commit, retransmitting the message, which will work, but may be problematic. Is it a good practice ?

Another solution is to merge these lines afterwards summing the counter. But what if another instance will increase the counter in the middle of the update.

What if two instances try to increase the counter ? @Version should be enough?

I’m not sure what is the proper solution here. How would you handle such cases ?

Also can you suggest some books about concurrency practices (not about the use of synchronized as I need to support Java EE and may run a cluster of application servers)?


Update:
After reading more about EJB and JPA I suppose I want something like an locking entity. For example I can create a new table with only id and key columns and data like this:

ID | KEY
1  | WORDS_CREATE_LOCK

So that when I need to handle a new word I will do something like this (not exact code, not sure it will even compile):

// MAIN FUNCTION
public void handleWord(String wordStr) {
  Word w = getWord(wordStr);

  if (w == null)
    w = getNewOrSychronizedWord(wordStr);

  em.lock(w);
  w.setCounter(w.getCounter() + 1);
  em.unlock(w);
}

// Returns Word instance or null if not found
private Word getWord(String wordStr) {
  Word w = null;

  Query query = em.createQuery("select w from words as w where w.string = :wordStr order by w.id asc");
  query.setParameter("wordStr", wordStr);
  List<Word> words = query.getResultList();

  if (words.getSize() > 0)
    w = words.get(0);

  return w;
}

// Handles locking to prevent duplicate word creation
private Word getNewOrSynchronizedWord(String wordStr) {
  Word w = null;
  Locks l = em.find(WORDS_CREATE_LOCK_ID, Locks.class);
  em.lock(l);

  Word w = getWord(wordStr);

  if (w == null) {
    w = new Word(wordStr);
    em.persist(w);
  }

  em.unlock(l);
  return w;
}

So the question is will it work that way? And can I do the same without maintaining a DB Table with locking rows? May be some Java EE container locking mechanism ?

If it helps I’m using JBoss 4.2.


I have a new idea for this. I can create two MDBs:

1st MDB with many instances allowed, that will handle all the messages and if the word is not found will send the word to the second MDB

2nd MDB with only one instance allowed, will handle the messages serially and will allow creation of new word

The best part: no entire table/method/process locking, only row locking on counter update

How good is that ?

Thanks.

  • 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-11T17:48:50+00:00Added an answer on May 11, 2026 at 5:48 pm

    If you are looking for performance, no locking, etc. I would suggest to have another table: (word, timestamp). Your MDBs will just insert the word and the timestamp. Another process will count and update the table with the totals.

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

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I used a lambda function, as Matthew suggested. Here is… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer Yes, this installs the full 3.5 framework and SP1. The… May 11, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer The follwing should work. I just wrote something similar 3… May 11, 2026 at 7:04 pm

Related Questions

Was looking to get peoples thoughts on keeping a Lucene index up to date
I have recently discovered message selectors @ActivationConfigProperty( propertyName=messageSelector, propertyValue=Fragile IS TRUE) My Question is:
I have been trying to read a picture saved in Access DB as a
I have an application containing 4 MDB's each of which receives SOAP messages over
I'm attempting to create my first real C# application - a small pet project

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.