Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading application scope variables on startup.

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read a few different ways to load application scope objects/attributes on start up. Whats the best way?

Right now I'm extending the action servlet and overriding the initOther() method. I'm not sure how to access the datasources this way though so I hard coded a connection to the database through use Class.forName("driver");
Connection c = Drivermanager.getConnection(...

Though in the rest of my application I use getDataSource and the struts-config file to configure the database application.

I really don't like this duplication of code (especially when it comes the database password/account which can change), I'd prefer to leave it in the config file.

If there is a way to access the datasources that are in the config file from the actionservlet that I extended, that would be great. If there is some other way I should be doing this, can someone point me to a quick example?
I've read a little about writing your own plugin for startup/shutdown processes but I haven't found any "how to" do this (other than implement the plugin interface, and frankly the documentation I've found on this seems lacking).

any help would be appreciated,

-Tad
 
author
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure that I'm about to give you the answer you are looking for. I have pretty well given up accessing the database in the web layer. I find that it couples me to the database implementation and raises all the same kind of issues you are fighting. You are doing things like extending the ActionServlet and looking for places to cache things. Moreover, if you need to change from Struts to something else (yikes), then you need to re-invent everything you have done. Moreover, you might need to create a parallel user interface (say some kind of command-line interface for a batch job). Since data access has been coupled to the web layer, then you are force to re-invent the whole thing.

I think a better option is to create a layered application with clearly defined layers of responsibilities. The web layer (i.e. Struts) is responsible for accessing the model and presenting data. The model can be further sub-divided into a business layer (to hold business rules) and a persistence layer (the mechanics to access data). This means that you can change the type of database (or even from a database to a file) without touching Struts or your business layer. This strategy leads to a highly maintainable application and one that could be developed quicker (IMO).

There are persistence frameworks to do all the grunt work you are now trying to do. You can let the framework do the dirty work so you can spend your time writing other logic. A choice of a persistence framework is whole discussion in itself, but an easy one to get started with is iBATIS.

However, if you still want to cache things on start-up, you can do that with a Struts plug-in. It parallels the ActionServlet lifecycle. All you need to do is implement the plugin interface and register it in the struts-config.

I hope this helps
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there an easy how/to for writing a plugin? And is there a nice way to reference dataSources defined in the struts-config inside the plugin?

I really wanted to try to use something like hibernate, but there really isn't /wasn't enough time to implement it.

I sort of got thrown onto a project that started out as all jsp/servlets with a mysql backend that was halfway done. We finished but after listening to me complain about how messy it was everyone decided to re-design it using struts (and learn struts along the way) but now there isn't time to try add in layer for object persistence.

When I'm not working seven ten-hour day work weeks I plan on learning how to use a persistance framework at home.

I'd love some suggestions on free/open-source frameworks for object persistence to try. Is ibatis free?

-Tad
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hibernate is a very famous open source persistance /object relational mapping framework.
while ibatis (open source and already part of apache) is a sql mapper tool.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the links, the ibatis sounds interesting and the faq answered a couple of questions for me about "object/relational/mapping"

So with hibernate, you would not be able to have an object map to columns across different tables? (well thats what the ibatis faq led me to believe).

oh well I suppose this is all offtopic for this section.

Thanks for the info.

-Tad
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tad,

Another option you want to consider for wiring up your business/persistence/ui layers is Spring. Spring is a framework whose purposes are to provide glue logic via Inversion of Control and to make working with other J2EE services much easier. Spring has a nice plug-in for working with Struts. The source code for the plug-in in the org.springframework.struts package is worth a look.

A big plus of working with Spring is that it makes working with JDBC, IBAtis, Hibernate, JDO and several other ORM frameworks MUCH easier than using them directly as it manages the connections/sessions for you so you can focus on your application's logic instead of reinventing the troublesome and error prone plumbing code. It also provides a hierarchy of DataAccessExceptions that are common to all of its data access strategies. This eases your task as well as you don't have to decipher SQL error codes or dig deeply into the particular persistence frameworks own set of exceptions except when you absolutely need to.
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added that to my list of things to play with at home(ibatis and hibernate, possibly torque/turbine), when this project is over and I'm not working 65+ hour weeks.


-Tad
 
Alas, poor Yorick, he knew this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic