The following (incomplete) code works in an entity bean, but should work as
well in a session bean.

The tricky part was to figure out the name of the data source
("java:/DefaultDS"). 

That name is defined in the jboss.jcml configuration file:
        <mbean code="org.jboss.jdbc.XADataSourceLoader"
name="DefaultDomain:service=XADataSource,name=InstantDB">
        <attribute name="PoolName">InstantDB</attribute>

/*********************** Database Methods *************************/
    private static final String DS_NAME = "java:/DefaultDS";
    private Connection    _connection;

    private void makeConnection() throws NamingException, SQLException
    {
        InitialContext ic = new InitialContext();
        DataSource ds = (DataSource) ic.lookup(DS_NAME);
        _connection =  ds.getConnection();
    }

    private void closeConnection()
        throws SQLException
    {
        _connection.close();
    }

The following methods would contain standard JDBC code:

    private void insertRow ()   {}
    private void deleteRow ()   {}
    private void loadRow   ()   {}
    private void storeRow  ()   {}



The above methods make/closeConnection are called from
set/unsetEntityContext, respectively:

    public void setEntityContext(EntityContext context)
    {
        _context = context;
        try
        {
            makeConnection();
        }
        catch (Exception ex)
        {
            throw new EJBException("Unable to connect to database. " +
ex.getMessage());
        }
    }

    public void unsetEntityContext()
    {
        try
        {
            closeConnection();
        }
        catch (SQLException ex)
        {
            throw new EJBException("unsetEntityContext: " +
ex.getMessage());
        }
    }

This is probably different in a session bean.

Hope that helps...

Ga�tan Sheridan






-----Original Message-----
From: Clara Yuan [mailto:[EMAIL PROTECTED]]
Sent: June 27, 2001 16:31
To: [EMAIL PROTECTED]
Subject: [JBoss-user] how to connect to db using stateless session bean


I am trying to connect my sybase database from a
stateless session bean. it is very simple, I have
setup the jboss.jcml according to whatever the
tutorial said. but what do I need to do with the
ejb-jar.xml? and how to code in the sessionbean to
create a connection?
basically there is no sample code to do that. if
someone can post a code sample it will be very
helpful.
thanks,
Sara

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to