0

I have to persist of flow of continuously incoming messages. For performance reasons, I wait until I have a reasonable set of messages before sending them to the database. I keep these messages that are to be persisted in a collection C. I get notified by JMS whenever a message is added in the database.

I'd like to make a query on both the objects present in the database and the ones that are in C.

Is there a common design pattern to solve this issue ? Do I have to keep a copy of the database in a collection in memory ? If I try to read the contents of the database, and then the contents of C, and then put them together to make my query on this fusion, how can I make sure that while doing that another thread has not emptied C and put it in the database ?

Thanks

2
  • 1
    could you please explain with an example maybe? I'd like to make a query on both the objects present in the database and the ones that are in C what do you mean by this? elaborate a little. Commented Mar 5, 2013 at 6:22
  • Why do you need to combine both the DB and the queue? How will the messages be commited to DB? Update? Insert? Delete? How complicated is the query? Commented Mar 5, 2013 at 22:43

2 Answers 2

1

If you want to query the data, (presumably, SQL SELECT), you will have to flush the collection to the DB.

You have at least a couple choices:

1) Lock up the DB access with a mutex. When you need to query, lock, flush collection to DB, query, unlock.

2) Design in a message for the input queue of the thread that handles the collection/DB that instructs it to flush the collection to the DB, signal a 'FlushComplete' condvar when flushed and then wait on another 'Continue' condvar. Queue up the message, wait on FlushComplete, query, signal Continue.

I would probably go with (2).

Sign up to request clarification or add additional context in comments.

Comments

0

Have you try to use AKKA? It is a really easy to use agent system that can solve your pb easily and give some good design patterns when you want to use asynchron system and multithreading

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.