Results 1 to 2 of 2

Thread: Replacing META-INF/context.xml database configuration

  1. #1
    Join Date
    Sep 2009
    Posts
    6

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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).

    Code:
    private DataSource ds;
    
    public void setDataSource(DataSource dataSource) {
     this.ds=dataSource;
    }
    I also suggest a read of the reference guide which has a small section on this.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •