Skip to main content

Sharing a Connection between EJB's

14 replies [Last post]
rong999
Offline
Joined: 2007-08-26

For example: I have 2 stateless EJBs: EJB1 and EJB2 and EJB1 calls EJB2. How can I share the same connection between these 2 EJB's without using Transactions and without using Entities?

We are planning to use Transactions and Entities in the future but as for our upcoming version, we need a different solution.

Thanks,
Ron

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jr158900
Offline
Joined: 2005-04-13

Within a transaction, when EJB1 calls EJB2 and EJB2 also acquires a connection, connection sharing will happen. i.e., same physical connection as that of EJB1 will be used.

Refer
https://glassfish.dev.java.net/javaee5/docs/DG/beamj.html#beams
http://blogs.sun.com/JagadishPrasath/entry/connectionsharinginglassfish

eg:

Tx : Container Managed
TxAttribute : REQUIRED

EJB1.method1()
{
acquireConnection()
doDBOperaitons()
closeConnection();
ejb2.method2();
}

Tx : Container Managed
TxAttribute : REQUIRED

EJB2.method2()
{
acquireConnection() // will get same physical connection as EJB1.method1().
}

rong999
Offline
Joined: 2007-08-26

The shared connection behavior is applicable only for Glassfish or also for other application servers?

Thanks,
Ron

jr158900
Offline
Joined: 2005-04-13

Yes, it is applicable for all appservers.

ejb-jar.xml 's resource-ref has the sharing scope : default : shareable

DataSource
javax.sql.DataSource
Container
[b] Shareable[/b]

km
Offline
Joined: 2005-10-28

If there are no transactions involved, why wouldn't you just leverage the connection pool and get
the connection from there?

- Kedar

rong999
Offline
Joined: 2007-08-26

Thanks, but what do you mean by leverage the connection pool? Do you mean somthing like creating a wrapper class for the connection pool with a HashMap of connections and this class is responsible to give the same connection for the two EJB's?

Thanks,
Ron

km
Offline
Joined: 2005-10-28

GlassFish provides you a Connection Pool. The way it is available to you is through the data-source
definition. Thus, you lookup something like "jdbc/MySQLDB" and you get a connection from the
connection pool that points to that Database. All the connections in connection pool already
implement the Connection Interface (http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Connection.html).

But since these are two session beans, do you have to have the same connection?

Maybe I am misunderstanding something ...

Regards,
Kedar

rong999
Offline
Joined: 2007-08-26

exactly, when one ejb calls another ejb and both of them use a connection, I want both of them to use the same connection. Is it possible without using transactions and without using entity?

Thanks,
Ron

Cheng Fang

When you call getConnection() on a DataSource, you always get the same
connection (may not be the same object instance, but they are equal).

To avoid using transaction, you will need to use bean management
transaction for EJBs. You may also want to consider setting
non-transactional-connection to true for your connection pool (see
domain.xml).

For example,

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class FooBean implments Foo {
@Resource(name="jdbc/trading", mappedName="jdbc/trading")
private DataSource tradingDB;

public void doFoo() {
Connection con = null;
try {
con = tradingDB.getConnection();
//...
} catch(...) {

} finally {
//close con
}
}

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class BarBean implments Bar{
@Resource(name="jdbc/trading", mappedName="jdbc/trading")
private DataSource tradingDB;

public void doBar() {
Connection con = null;
try {
con = tradingDB.getConnection();
//...
} catch(...) {

} finally {
//close con
}
}

-cheng

glassfish@javadesktop.org wrote:
> exactly, when one ejb calls another ejb and both of them use a connection, I want both of them to use the same connection. Is it possible without using transactions and without using entity?
>
> Thanks,
> Ron
> [Message sent by forum member 'rong999' (rong999)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241444
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

rong999
Offline
Joined: 2007-08-26

Thanks for your detailed answer. My problem is that my pool of connection is very limited (for example: 10 connections). When EJB1.xxx() method calls EJB2.yyy() method, it means that I will use 2 out of my 10 connections and this is what I want to avoid. I want, in this situation, to use only one connection. I'm not sure that bean management transaction is the solution for me or is it?

Thanks,
Ron

Cheng Fang

glassfish@javadesktop.org wrote:
> Thanks for your detailed answer. My problem is that my pool of connection is very limited (for example: 10 connections). When EJB1.xxx() method calls EJB2.yyy() method,
EJB1.xxx() first closes the connection (meaning returning it to pool),
then call EJB2.yyy(), which gets another connection from the pool. So
they are only using 1 at any given time.
> it means that I will use 2 out of my 10 connections and this is what I want to avoid. I want, in this situation, to use only one connection. I'm not sure that bean management transaction is the solution for me or is it?
>
>
Using bean managed transaction is a separate topic from using
connections. With bean managed transaction, you can choose not to
involve any transaction, which I guess is what you want to achieve.
Otherwise, ejb container will automatically demarcate transactions.

-cheng
> Thanks,
> Ron
> [Message sent by forum member 'rong999' (rong999)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241528
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

rong999
Offline
Joined: 2007-08-26

Actually I don't need transactions - I need to reduce the number of Connections in the applications. When there is a call from one EJB method to another EJB method, 2 connections are used (each EJB has XXXDataSource.getConnection) . What I'm asking, is how can I share that the same Connection (same object!) will be used on both EJBs. My concern is not transactions!!! My concern is that I have limited amount of connections in my connection pool. Is it possible without developing somthing of my own or can I use a feature from the application server?

Cheng Fang

> When there is a call from one EJB method to another EJB method, 2 connections are used (each EJB has XXXDataSource.getConnection) .

As I said in previous reply, only 1 connection is used at runtime, not
2, if you close it properly after each use. Connection objects are all
stateless. All connections from the same pool are considered the same.
Why do you care they are the same object? These are all low-level
details that are better handled by appservers. The very purpose of
connection pool in appserver is to make efficient use of connection
resource, which is what you want.

Another (bad) option is to pass the connection as method param from ejb1
to ejb2, if it's a local call. I would not do it this way, but at least
you can make sure they are the same object. If you still have
questions, some sample code may help us better understand your use case.

-cheng

> What I'm asking, is how can I share that the same Connection (same object!) will be used on both EJBs. My concern is not transactions!!! My concern is that I have limited amount of connections in my connection pool. Is it possible without developing somthing of my own or can I use a feature from the application server?
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

rong999
Offline
Joined: 2007-08-26

you are absolutely correct. It just mean that I need to add a lot of conn.close() statements to my code (because of a move from old framework to EJB3). As for now it is good enough for me. But it can be nice to have a "right" way to make sure that one specific connection object is used with EJB calls without relaying on transactions. This could make my life easier converting my old application to EJB3.

thanks,
Ron

lancea
Offline
Joined: 2003-06-13

As cheng points out, grab the connection and release the connection at start/exit of your business methods and you should be on your way. Your connection pool should give you a clean connection for the given backend based on the DataSource you are using and there is zero reason that each EJB has a specific connection object, other than a connection object for backend that you are accessing.