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

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

    Comment

    Working...
    X