Hello Thibault

You will want to set this up in your init method of your plugin
so if your DataSource is setup with a key parameter is already identified as "database1"

<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource" key="dataBase1"> <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" /> <set-property property="url" value="jdbc:mysql://192.168.0.25/cvdunet" />
       <set-property property="username" value="jdbcuser"/>
       <set-property property="password" value="cvdunet" />
   </data-source>
</data-sources>

later in the code you retrieve desired connection as follows:

public void init(ActionServlet actionServlet, ModuleConfig config)
   throws ServletException
..
java.sql.Connection connection = servlet.findDataSource("database1").getConnection();

HTH/Bon Chance,
Martin-
----- Original Message ----- From: "Thibaut Lassalle" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Tuesday, September 13, 2005 10:29 AM
Subject: Re: access the DataSource in a plugin object


Martin

you are in an action therefore you have getDataSource(request) available
but i am in a plugin therefore i don't have request element.

Any idea ?
Thibaut

Martin Gainty a écrit :

Thibault-

/*This is straight from the Struts doc available at*/
http://www.jajakarta.org/struts/struts1.2/documentation/ja/target/faqs/database.html
/*for a single DataSource*/
public ActionForward
      execute(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) throws Exception
{
javax.sql.DataSource dataSource;
java.sql.Connection myConnection;
try {
 dataSource = getDataSource(request);
 myConnection = dataSource.getConnection();
 // do what you wish with myConnection
} catch (SQLException sqle) {
   getServlet().log("Connection.process", sqle);
} finally {
   //enclose this in a finally block to make
   //sure the connection is closed
   try {
      myConnection.close();
   } catch (SQLException e) {
      getServlet().log("Connection.close", e);
   }
  }
}

/*for multiple DataSources */
If you need more than one data source in a module, you can include a key attribute in the data-source element:
<data-sources>
  <data-source key="A" type="org.apache.commons.dbcp.BasicDataSource">
     ... properties as before ...
  </data-source>
  <data-source key="B" type="org.apache.commons.dbcp.BasicDataSource">
     ... properties as before ...
  </data-source>
  ...
</data-sources>
Which can then be accessed by including the key ("A" in this case) as an additional parameter to the Action.getDataSource() method.
from inside your Action Class (prefereably the execute method)


  try {
     dataSourceA = getDataSource(request, "A");
     dataSourceB = getDataSource(request, "B");
 }

Each module can have as many data sources as it needs. The keys only need to be unique within a module since the struts module system maintains a name space for the items in each module to protect you from name clashes.

HTH,
Martin-

----- Original Message ----- From: "Thibaut Lassalle" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Tuesday, September 13, 2005 5:02 AM
Subject: access the DataSource in a plugin object


hi

I would like to access the DataSource object from a plugin in my Struts application.
I use struts 1.2 with modules.

In the root struts-config.xml the data source is discribe as :

<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource" key="dataBase1"> <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" /> <set-property property="url" value="jdbc:mysql://192.168.0.25/cvdunet" />
       <set-property property="username" value="jdbcuser"/>
       <set-property property="password" value="cvdunet" />
   </data-source>
</data-sources>

And in my plugin, i call this :

DataSource aDataSource = (DataSource) actionServlet.getServletContext().getAttribute (Globals.DATA_SOURCE_KEY);


but 'aDataSource' is 'null'.
Do you know the right way to access the DataSource ?


Thanks
Thibaut Lassalle


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



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




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



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

Reply via email to