Dave, which version of struts are you using?

Here is how I access my datasource with a nightly build from ~ Jan 20.

  <data-sources>
    <data-source>
      <set-property property="autoCommit"
                    value="false" />
      <set-property property="description"
                    value="example connection pool" />
      <set-property property="driverClass"
                    value="oracle.jdbc.driver.OracleDriver" />
      <set-property property="maxCount"
                    value="4" />
      <set-property property="minCount"
                    value="2" />
      <set-property property="password"
                    value="target" />
      <set-property property="url"
                    value="jdbc:oracle:thin:@machine:port:sid" />
      <set-property property="user"
                    value="username" />
    </data-source>
  </data-sources>

This is how I access it

Subclass of org.apache.struts.action.ActionServlet

    java.sql.Connection conn = null;
    javax.sql.DataSource dataSource = 
        (javax.sql.DataSource)
this.getServletContext().getAttribute(org.apache.struts.action.Action.DATA_S
OURCE_KEY);
    try {
      conn = dataSource.getConnection();
    } catch (Exception e) {
        ...
    }


In my action classes I pull the database from the servlet

    try {
      dataSource = (javax.sql.DataSource)
servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
      myConnection = dataSource.getConnection();
      ...
    } catch ...

Notice that the key is the org.apache.struts.action.Action.DATA_SOURCE_KEY
which
is the default key assigned to the datasource if you don't set the key
property.
The default is fine for my app which is only using one datasource.  If you
need
multiple you need to define the key in the data-source node of the XML.

Hope this helps

Chris


-----Original Message-----
From: Crazy Dave [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, February 20, 2002 7:12 AM
To: Struts Users Mailing List
Subject: (StrutsNewUser) - Accessing datasource in an initialisation
servlet.


Web.xml extract
<!-- Application Initialization Servlet -->
<servlet>
<servlet-name>applicationInitialisation</servlet-name>
<servlet-class>Blah.ApplicationInitServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>

I wish to load information into an arraylist of beans
for use in <html:select>, the data comes from
database.

I need to know how to get access to the datasource in
a standard servlet.

I've tried extending my class from ActionServlet, but
the datasource keeps returning null.

see below:

ServletContext sc = getServletContext( );
DataSource ds = findDataSource( null );

try {
SecurityModuleSql sql = new SecurityModuleSql( ds );
sc.setAttribute(key, sql.getDropDown( ) );
} 

Cheers Dave

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com

--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

Attachment: msg24248/bin00000.bin
Description: application/ms-tnef

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to