users@glassfish.java.net

Re: Sharing a Connection between EJB's

From: Cheng Fang <Cheng.Fang_at_Sun.COM>
Date: Mon, 22 Oct 2007 10:30:37 -0400

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_at_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_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>