File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Can session beans interact?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Can session beans interact??" Watch "Can session beans interact??" New topic

Can session beans interact??

Fiona Healy

Joined: Jan 27, 2005
Posts: 21

i am using two databases A and B, one to pull data from (call it A) and the other to store this data (call it B).

my application is supposed to generate reports
so i have a fields EJB and a report EJB and 2 corresponding session beans

These beans have a many to many unidirectional relationship
a report has many fields and fields can be associated with many reports
so i have a link in the application DB to represent this (B)

I am accessing database A using JDBC in a Session bean called dataAccess

How can i populate the fields EJB using the data in database A?

Does the dataAccess session bean have to talk to the fields session bean?
Can two session beans talk to each other and pass data to each other?

please help!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33098

Originally posted by Fiona Healy:
Two session beans can talk to each other as long as they are in the same enterprise application. It doesn't matter what they do or what databases they interact with.

Since you are using JDBC with database A, you get it back into a Java data structure (be it Strings or a data transfer object of your creation.) If you are using JDBC in database B, just write it the way you normally do.

If you are using entity beans, you can call ejbCreate() to add a new row to the table in database B. If it is an existing row, you can call a finder to get that entity bean and then setters for the updates.

Note that you will want to use an XA driver to support transactions across the two databases.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Dave Clark
Ranch Hand

Joined: Feb 16, 2005
Posts: 52
two session beans can talk to each other even if they're part of different enterprise apps, or even if they're running on different servers (if they have remote interfaces or web services interfaces). they only have to be a part of the same enterprise app if they *only* have local interfaces. that said however, you don't seem to have any good reason not to have them be part of the same enterprise app.

just to take a step back however - you say that you have a 'fields EJB' and a 'report EJB' - I assume from this you actually mean that you've got an 'Field' Entity Bean , and a 'Report' Entity Bean. If this is the case, then there's no need at all to have the 2 Session Beans that you also have talking to each other at all.

It sounds like what you really want is to have a Session Bean talk to your 'A' database (via JDBC, or via your existing Session bean if that's what you want), then create instances of the Entity Beans that are associated with your 'B' database (the container will call ejbCreate() after you call create() on the local or remote home interface).

And yes - an XA driver will allow you to have distributed 2-phase commit transactions across the 2 databases.



Dave Clark<br />Senior WebSphere Architect<br /><a href="" target="_blank" rel="nofollow">Versant Open Access - JDO2 & EJB3</a>
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704

I�d like to suggest you a different approach. If all you need to do is to move data from one database to another, then using J2EE and EJB is maybe too much overhead. I would suggest you to use a totally different toll like openadaptor (, which is free. This will let you move data from one data storage system (like database, ldap, jms, etc) to any other storage. It is very generic and the good part is that you don�t need to write any code; providing an appropriate configuration file is quite enough. It has a lot of interesting features (including filters and transformation, message hospitals, etc) and for your application could be a very fast and reliable solution.
I hope this will help.

I think, therefore I exist -- Rene Descartes
Fiona Healy

Joined: Jan 27, 2005
Posts: 21
So what you are saying is to have one session bean interacting with the DB that holds the data that i want to transfer and the entity bean (Fields) to which i want to transfer the data to???

Once this is done, i have a scenario where there is a many to many unidirectional relationship. i.e. ReportEJB has many FieldEJB and FieldEJB can be associated with many ReportEJB. Reports need to know about fields but not the other way around

I have followed numerous examples on how to configure both the jbosscmp file and the ejb-jar file in order for two beans to have a many-many uni-directional relationship. I think i have done this succesfully as no errors are being thrown.

All my tables are building accordingly and i have my abstract accessors specified in both beans concerned but when i go to create a new report bean the link table is not populated.

Does it matter that FieldEJB is already populated and has read-only access?

Is there any examples out there with the actual manipulation of the data in the database as opposed to how to configure the META-INF files.

Also can i use remote beans in these relationships or does it have to be local???

please help
I agree. Here's the link:
subject: Can session beans interact??
It's not a secret anymore!