Announcement Announcement Module
No announcement yet.
Replacing META-INF/context.xml database configuration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replacing META-INF/context.xml database configuration

    Hi, I am trying to introduce spring into an older tomcat based web application in order to use CXF.

    Question #1: I have to change the way the database is configured because META-INF/context.xml isn't supported. Is that right?

    Question #2: If the answer to #1 is yes, can I change the configuration and still use all the old/straight jdbc code?

    Ideally we would refactor all of the existing jdbc code to use at least SimpleJdbcTemplate or something better. But there is no time to do this before release.

    Question #3, If the answer to #2 is yes, What would the equivalent configuration be?

    The existing context.xml has the configuration for 2 database connections

    <Resource name="jdbc/users" url="..." username="..." password="..." ... type="javax.sql.DataSource" />
    <Resource name="jdbc/docs" url="..." username="..." password="..." ... type="javax.sql.DataSource" />

    I *think* I want to add to the beans.xml class

    <jee:jndi-lookup id="usersDataSource" jndi-name="jdbc/users" resource-ref="true" />
    <jee:jndi-lookup id="docsDataSource" jndi-name="jdbc/docs" resource-ref="true" />

    But are those the right idís to use?

    The old code uses

    InitialContext ic = new InitialContext();
    Context envContext = (Context) ic.lookup("java:/comp/env");
    ds = (DataSource) envContext.lookup(dataSourceName);

    where dataSourceName is ďjdbc/usersĒ

    So would this need to change? Iím a bit confused because based on what I have, the idís in the beans.xml arenít used. Can that be right?

    Any advice appreciated. Iím sure there are others who are having trouble with these same simple conversions.

  • #2
    Please use [ code][/code ] tags when posting code...

    1) Wrong, you can still define your datasources in the context.xml and use jndi to locate them.
    2) yes, with a minor refactoring
    3) Wrap the datasource in a TransactionAwareDataSourceProxy, this is aware of the spring transaction and already ongoing connection/transaction.

    You need to at least refactor your code to not do the lookup like you posted but use a datasource directly and have that injected (the wrapped one).

    private DataSource ds;
    public void setDataSource(DataSource dataSource) {
    I also suggest a read of the reference guide which has a small section on this.