Announcement Announcement Module
No announcement yet.
Integrating Spring JDBC and WebWork2 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integrating Spring JDBC and WebWork2

    I've got WebWork2 and Spring's JDBC stuff working great. Thanks to some help from Thomas, I'm even pulling MetaData through Spring.

    I love it.

    Now some questions about best-practices when using these two together...

    1) Where in WebWork2 should I create my datasource object? Should I create one at session start and store it in WebWork's session object? Or maybe application-start and store it in WebWork's application object? Can I share Spring datasources among WebWorks action invocations like that?

    2) I'm using DriverManagerDataSource to create my datasource. I'm a little unclear about whether this is really what I want.

    I don't need/want JNDI for finding datasources (at least I don't think so) and I don't need transactions (at the moment). But am I missing out on something else important or, perhaps, shooting myself in the foot by going this route?

    3) Am I getting pooled connections when asking for datasources from DriverManagerDataSource? Or do I have to do something else?


    - Gary

  • #2
    1. I would recommend creating your DataSource object with Spring IoC. Spring is much more capable in the middle tier than WW, which is why many WW users (including Atlassian) use WW + Spring with great success. Although if you want just to use Spring JDBC as a class library, as it seems you're doing, of course that will work fine. Configuring your DataSource with Spring will provide benefits such as the ability to transparently switch between "local" and container (JNDI) DataSources; the ability to apply AOP advice etc.

    I'm not 100% clear on what you're asking overall in the rest of this question. DataSources will normally be of application rather than Session scope. You should be able to make a DataSource available to WW actions via DI, but I would advise keeping anything that uses a DataSource well below the UI tier. DataSources are threadsafe.

    2) I'm using DriverManagerDataSource to create my datasource. I'm a little unclear about whether this is really what I want.
    This is definitely not what you want in any real application. You're not getting pooling, meaning the overhead of establishing a connection is extremely high. This won't scale.

    Fortunately there's a simple solution: use a connection pool such as Apache Commons DBCP, or use a container data source. This is transparent to code such as Spring JDBC that uses the connection. Commons DBCP or the like is most likely the best solution. Look at Spring samples such as the PetStore to see how this can be configured.

    Hope this helps,