Results 1 to 2 of 2

Thread: Wrapping JdbcTemplate with TransactionProxyFactoryBean

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    Default Wrapping JdbcTemplate with TransactionProxyFactoryBean

    hello,

    I am using Spring inside an EJB (on Weblogic). Until now I was using one database only and everything was nice. The EJB had a 'transaction required' attribute. Now I have to run also a query against an archive database inside one method. As this is a SELECT to a read-only database then I don't want XA connections. So I thought I could suspend the production DB transaction while running the archive query. I am using JdbcTemplate (through JdbcOperations interface). As this is a simple query, I didn't want to create a separate bean for that. I tried to wrap the JdbcTemplate with TransactionProxyFactoryBean. I have these declarations:

    Code:
      <bean id="archiveDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
          <value>jdbc/ARCH</value>
        </property>
      </bean>
    
      <bean id="archiveJdbcOperations" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg><ref bean="archiveDataSource"/></constructor-arg>
      </bean>
    
      <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
    
      <bean id="wrappedArchiveJdbcOperations" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="target"><ref bean="archiveJdbcOperations"/></property>
        <property name="transactionAttributes">
          <props><prop key="*">PROPAGATION_NOT_SUPPORTED</prop></props>
        </property>
      </bean>
    This seems to work ok. Just a few questions:
    • is it a good idea to wrap JdbcTemplate like this?
    • although my code works, there is one exception internally - when the JdbcTemplate initializes, it uses the SQLErrorCodesFactory to get database metadata and the TransactionProxyFactoryBean is not working then yet and Weblogic throws an exception:
      Code:
      Connection has already been created in this tx context.
      How could I get around this? I know that my database is Oracle so maybe I could somehow tell my JdbcTemplate that and skip the metadata loading?


    thank you,
    erik

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You can of course just call out to another service object that is wrapped as you need it, but I don't see anything that bad with wrapping one template instance like this and using it.

    As for the error, you should be able to easily subclass JDBCTemplate and override the afterPropertiesSet and getExceptionTranslator methods (might have to make it synchronized if all you do is get it later, as opposed to just setting it yourself to the Oracle one first).
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 3
    Last Post: Mar 1st, 2010, 05:45 PM
  2. Replies: 15
    Last Post: Jun 3rd, 2005, 08:19 AM
  3. CMT, JdbcTemplate and connection pools
    By jayschm in forum EJB
    Replies: 3
    Last Post: Apr 25th, 2005, 08:11 AM
  4. Replies: 8
    Last Post: Jan 9th, 2005, 10:24 AM
  5. Injecting JdbcTemplate instead of just DataSource?
    By ArtVandelay in forum Container
    Replies: 4
    Last Post: Oct 20th, 2004, 10:22 PM

Posting Permissions

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