Announcement Announcement Module
No announcement yet.
Wrapping JdbcTemplate with TransactionProxyFactoryBean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Wrapping JdbcTemplate with TransactionProxyFactoryBean


    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:

      <bean id="archiveDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
      <bean id="archiveJdbcOperations" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg><ref bean="archiveDataSource"/></constructor-arg>
      <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>
    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:
      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,

  • #2
    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).