You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Shaun Whyte <sw...@opaltelecom.co.uk> on 2002/07/24 13:45:43 UTC
Using Data-source's with Struts
I am trying to find a good way of using the data sources within a Stuts
application.
Supposing I have some Manager classes (like Facades); each of which have
methods which perform a set of related use-cases, which abstract complexity
of communicating with the beans. The idea being that a Struts Action class
would use the manager classes to perform the detailed operations, and
therefore not only can a Stuts Action class use the facades (manager
classes), but any other class which needs to perform the same functionality
can also use them (e.g. it may not be an HTTP request). My manager classes
are created in a sub-class of ActionServlet and stored in the servlet
context for use by Struts actions. If it is not a Struts application which
is not using them, then they are created in some other way.
With regards to the DataSources, supposing my manager class needs to use
data-source(s). I don't want my manager classes to be tied to HTTP, so I
don't want them to look in the ServletContext for the data source, which is
where Struts stored them.
Is the best solution, to create a List of the DataSource object in my
sub-class of ActionServlet, then when I create the managers, just pass in
the list for them to use. That way, if another type of application which
needs to use the managers (which isn't using Struts), it would create the
DataSources itself, put them in a list, and, similar to my ActionServlet,
pass them in to the manager classes when it created them.
If anyone has any ideas for me, or if this is a good way of doing things,
please reply.
Also as a second question, I have been looking at the JavaDoc's for the JDBC
Connection Pool (from bitmechanic.com), and cannot find a class which
implements javax.sql.DataSource to use within Struts, is this correct?
Many thanks!
Shaun.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Using Data-source's with Struts
Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Wed, 24 Jul 2002, Shaun Whyte wrote:
> Date: Wed, 24 Jul 2002 12:45:43 +0100
> From: Shaun Whyte <sw...@opaltelecom.co.uk>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Using Data-source's with Struts
>
> I am trying to find a good way of using the data sources within a Stuts
> application.
>
> Supposing I have some Manager classes (like Facades); each of which have
> methods which perform a set of related use-cases, which abstract complexity
> of communicating with the beans. The idea being that a Struts Action class
> would use the manager classes to perform the detailed operations, and
> therefore not only can a Stuts Action class use the facades (manager
> classes), but any other class which needs to perform the same functionality
> can also use them (e.g. it may not be an HTTP request). My manager classes
> are created in a sub-class of ActionServlet and stored in the servlet
> context for use by Struts actions. If it is not a Struts application which
> is not using them, then they are created in some other way.
>
> With regards to the DataSources, supposing my manager class needs to use
> data-source(s). I don't want my manager classes to be tied to HTTP, so I
> don't want them to look in the ServletContext for the data source, which is
> where Struts stored them.
>
> Is the best solution, to create a List of the DataSource object in my
> sub-class of ActionServlet, then when I create the managers, just pass in
> the list for them to use. That way, if another type of application which
> needs to use the managers (which isn't using Struts), it would create the
> DataSources itself, put them in a list, and, similar to my ActionServlet,
> pass them in to the manager classes when it created them.
>
> If anyone has any ideas for me, or if this is a good way of doing things,
> please reply.
>
To use a Struts-provided data source, you need to make it accesible to the
business logic that needs it. That means you need to do one of the
following:
* Pass the data source instance as a method parameter on some call
* Pass a reference to the ServletContext at some point, so that the
business logic can look up the appropriate attribute itself.
* Make the data source available through a well known static variable.
None of these are very attractive, but that's pretty much the choices.
If you are running on a J2EE app server (or Tomcat 4, which also supports
JNDI namespaces), I strongly suggest that you consider using the container
provided data sources, instead of the ones provided by Struts. A typical
use of such a data source in a business logic class looks like this:
InitialContext context = new InitialContext();
DataSource ds = (DataSource)
context.lookup("java:comp/env/jdbc/MyDatabase");
Connection conn = ds.getConnection();
... do your thing ...
conn.close();
where "jdbc/MyDatabase" matches a <resource-ref> that you have defined in
your web.xml file. The actual database that this talks to is configured
as part of the deployment information for your app (in Tomcat, you put
stuff into server.xml for this).
But the important point is that this approach requires none of the above
alternatives for exposing the connection pool -- the container makes sure
that the "new InitialContext()" call always gives you the JNDI naming
context for the current application.
> Also as a second question, I have been looking at the JavaDoc's for the JDBC
> Connection Pool (from bitmechanic.com), and cannot find a class which
> implements javax.sql.DataSource to use within Struts, is this correct?
>
I don't know. If it does have a DataSource implementation, you might be
able to use this instead of the one in Struts - but you don't really need
both.
> Many thanks!
>
> Shaun.
>
Craig
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Using Data-source's with Struts
Posted by Robert Taylor <rt...@mulework.com>.
Shawn, our company had a need to access JDBC Connections
in web apps, stand alone applications, and for testing.
We have an object, ConnectionManager that operates in one
of three modes; dependent, stand alone, and manual. It
provides a simple API for accessing connections from
different data sources.
Each mode utilizes javax.naming.InitialContext so I can
look up the data sources in a standard manner.
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(dataSourceName);
In dependent mode, it assumes that an InitialContextFactory
has been defined in the System.properties and that the data
sources have been bound to the InitialContext by some other
object.
In stand alone mode, it sets the InitialContextFactory in
the System.properties, reads from a datasource-config.xml
file, creates the data sources, and binds them to the
InitialContext.
In manual mode, it sets the InitialContextFactory in
the System.properties and binds a set of default data sources to
the InitialContext. It also allows you to manually add data sources
to be bound to the InitialContext.
The ConnectionManager has 3 API calls for accessing Connections.
ConnectionManager.getConnection() // uses default dsn
ConnectionManager.getConnection(String dsn)
ConnectionManager.getConnection(String dsn,
String user, String pwd)
ConnectionManager is a singleton, and therefore is accessible
to any object via ConnectionManager.getInstance() so it can
be used in the web tier or business tier.
So you would basically have to subclass javax.naming.InitialContext
and javax.naming.spi.InitialContextFactory, and then write your
own object that manages data sources or Connections.
In your ServletContextListener, BootStrapServlet, main method, etc...
ConnectionManager cm = ConnectionManager.getInstance();
cm.setMode(mode);
cm.init();
So in your manager classes, you could just use do the following:
public void doSomething() throws Exception {
Connection conn = null;
try {
// assume the ConnectionManager has been initialized
conn = ConnectionManager.getConnection(dsn);
// do something
} catch (SQLException sqle) {
throw new Exception(sqle);
} finally {
// commit or rollback connection
}
}
If you think about it, you will have to write your own caching/lookup
mechanism any ways, why not use standard Java APIs and if your move
to application server that support JNDI, then your code does not have
to change, you just configure your data sources differently.
Any servlet container that conforms to the Servelt2.3 specification
will provide a way for you to utilize InitialContext to look up
data sources.
Take a look at
http://java.sun.com/j2se/1.4/docs/api/javax/naming/Context.html
There are probably many ways to skin this cat, but this has worked for us.
robert
> -----Original Message-----
> From: Shaun Whyte [mailto:swhyte@opaltelecom.co.uk]
> Sent: Wednesday, July 24, 2002 7:46 AM
> To: Struts Users Mailing List
> Subject: Using Data-source's with Struts
>
>
> I am trying to find a good way of using the data sources within a Stuts
> application.
>
> Supposing I have some Manager classes (like Facades); each of which have
> methods which perform a set of related use-cases, which abstract
> complexity
> of communicating with the beans. The idea being that a Struts
> Action class
> would use the manager classes to perform the detailed operations, and
> therefore not only can a Stuts Action class use the facades (manager
> classes), but any other class which needs to perform the same
> functionality
> can also use them (e.g. it may not be an HTTP request). My
> manager classes
> are created in a sub-class of ActionServlet and stored in the servlet
> context for use by Struts actions. If it is not a Struts
> application which
> is not using them, then they are created in some other way.
>
> With regards to the DataSources, supposing my manager class needs to use
> data-source(s). I don't want my manager classes to be tied to HTTP, so I
> don't want them to look in the ServletContext for the data
> source, which is
> where Struts stored them.
>
> Is the best solution, to create a List of the DataSource object in my
> sub-class of ActionServlet, then when I create the managers, just pass in
> the list for them to use. That way, if another type of application which
> needs to use the managers (which isn't using Struts), it would create the
> DataSources itself, put them in a list, and, similar to my ActionServlet,
> pass them in to the manager classes when it created them.
>
> If anyone has any ideas for me, or if this is a good way of doing things,
> please reply.
>
> Also as a second question, I have been looking at the JavaDoc's
> for the JDBC
> Connection Pool (from bitmechanic.com), and cannot find a class which
> implements javax.sql.DataSource to use within Struts, is this correct?
>
> Many thanks!
>
> Shaun.
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>