Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multithreaded Java SE application using Entity Beans

Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am fairly familiar with entity beans but I have only used them with an EJB container.

I have a SE application that uses an EntityManagerFactory to create an entity manager. I would like to multithread this application but I am very confused about using Entity Beans with multiple threads.

Here is a small example of the entites and what the app does:

Person (Entity Bean) - contains a base directory path, that path contains a set of directories associated with this person
Directory (Entity Bean) - contains a set of files associated with this diretory, also bidirectinal back to Person Entity
File (Entity Bean) - contains some file metadata, also bidirectional back to Directory Entity

The application (currently single threaded) - The app will parse only one persons directory/file structure (person name passed on command line). It gets an entity manager from its entity manager factory. Fetches the person entity by name. It then gets a list of all directories in the persons base path. It also gets a set of Directory Entities associated with that person. If the directory is out of date with the Directory Entity the Directory Entity is deleted, recreated and re-parsed, if there is no DirectoryEntity for a directory an entity is created and parsed.

There are not many people so I would really like to multi-thread at the directory level. IE when the app gets the Person Entity by name, I would like to fetch all the Directory Entities for that person. Then spawn a new thread for each Directory Entity. Each thread will be responsible for checking if the Directory Entity is out of date. The thread will then either
1. delete the Directory Entity (file system directory that maps to this entity has been removed),
2. create a new Entity Directory (if one does not exist) and parse metadata from all files
3. if out of date, delete all File Entities and re-created them with new metadata.

Some pseudo code

How do I make the DirectoryParser thread safe? Do I need to create a new EntityManager for each DirectoryParser, or can I share the EntityManager I have already created? Also each DirectoryParser thread will be adding/deleting/updating itself in a new transaction and since each Directory Entity points back to the person will the Person Entity get messed up?

Sorry for the long winded explanation, but I have no idea if what I want to do is possible.


  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic