Announcement Announcement Module
Collapse
No announcement yet.
TransactionAwareDataSourceProxy and legacy classes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TransactionAwareDataSourceProxy and legacy classes

    I have seen a few post on the forums regarding this situation but I am looking for guidance (fairly new to the framework). Your help is much appreciated (links, examples, etc)

    We have developed a framework using spring but now need to send legacy code/classes a connection for various reasons. I have read to use TransactionAwareDataSourceProxy, use the getConnectiond and send it to the class. Using this proxy transactions will be managed with spring along with closing of connections. On the right track?


    1) Below is what we are currently using for connections etc. What would be the next step to incorporating TransactionAwareDataSourceProxy or ... ?

    Code:
     <bean id="oracleDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName" value="java:/OracleDS"/>      
      <property name="cache" value="false"/>      
      <property name="proxyInterface" value="javax.sql.DataSource"/>
     </bean> 
    
        <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">   
       <property name="transactionManagerName"><value>java:/TransactionManager</value></property>
        </bean>
    
     <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
      <property name="transactionManager" ref="transactionManager" />
      <property name="transactionAttributes">
       <props>
        <prop key="insert*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
      </property>
     </bean>
    
     <bean id="daoFacade" parent="baseTransactionProxy">
      <property name="target">
       <bean class="com.fcci.ods.dao.DAOFacade">
    ...
     </bean>
      </property>
     </bean>

    2) What approch should be taken to inject the connection? Is there an easy xml configuration to inject the TransactionAwareDataSourceProxy.getConnection into a class?

    For example, the legacy class looks like:

    Code:
    public class LegacyClass {
    
      public LegacyClass (Connection connection) {
         this.connection = connection;
      }
      ...
    }

    Again, appreciate your help and guidance.

  • #2
    If you're using TransactionProxyFactoryBean this will bind a connection to threadLocal for you. All you need to do is inject the dataSource and you're away. I would have a look at the execute method of JdbcTemplate as this does it all for you.

    This will get the connection for you (you do need to release it). I would have a look at the mentioned class.
    Code:
    Connection con = DataSourceUtils.getConnection(getDataSource());

    Comment


    • #3
      Thanks karldmoore for your reply,

      The current 'legacy' code doesn't cleanup the connection. It just uses the connection and then closes the resultset.

      Code:
      			preparedStatement = connection.prepareStatement(formula);
      			preparedStatement.setInt(1, number);
      			ResultSet resultSet = preparedStatement.executeQuery();
      			while (resultSet.next()) {
      				ArrayList row = new ArrayList();
      				for (int x=1; x <= resultSet.getMetaData().getColumnCount(); x++) {
      					row.add(resultSet.getObject(x));
      				}
      				numberList.add(row);
      			}
      			resultSet.close();

      I was trying to find a way Spring (or...) could handle that - this way I wouldn't have to re-write a bunch of code that this time. It seem like TransactionAwareDataSourceProxy would accomplish this but I am wondering the best way to implement this witht he current connection, transactionmanager, and proxy.

      If I have mis interpreted something, please let me know. Thanks again.

      Comment


      • #4
        TransactionAwareDataSourceProxy is going to do pretty much the same thing.
        Code:
        Connection con = DataSourceUtils.doGetConnection(getTargetDataSource());
        Is there any chance you could give a few more details on what you are expecting or trying to do here. This code does exactly what you want, not sure what else you are looking for.

        Comment


        • #5
          Yes, I am familiar with

          Code:
          Connection con = DataSourceUtils.doGetConnection(getTargetDataSource());
          I am trying to find a way to manage the connection without changing legacy code or code that calls it just - Inject the connection into legacy bean and not have to worry about closing the connection ("you do need to release it") etc

          ie.

          Code:
          public class LegacyClass {
          
            public LegacyClass (Connection connection) {
               this.connection = connection;
            }
            ...
          }
          
          <bean id="legacyClass" class="com.whatever.LegacyClass">
          		<property name="connection" ref="oracleConnection" />
          </bean>

          If I am making this harder than I need to be, just say the word and I will move forward with another solution.

          Comment


          • #6
            Originally posted by musky View Post
            I am trying to find a way to manage the connection without changing legacy code or code that calls it just - Inject the connection into legacy bean and not have to worry about closing the connection ("you do need to release it") etc
            The thing is you normally inject a datasource and not the connection directly. Injecting an open connection and automatically closing it won't be that easy I fear.

            Jörg

            Comment

            Working...
            X