views:

296

answers:

1

I am working on profiling a legacy application using Spring AOP and I want to get some data points around a decision I need to make.

This is what I have done so far: I have created the code and configuration using Spring AOP which would help me to profile the beans that are being managed by the Spring container.

While profiling the dao layer, I find that the legacy app uses c3p0 framework - specifically, the ComboPooledDataSource class. Since this class is a final class, I am unable to work work with Spring AOP any longer- since cglib cannot create proxies for final classes!

Faced with this roadblock, I am planning to use aspect compile time weaving technique. I want to know if this is the right decision or are there other ways to overcome this problem using Spring AOP itself. Please let me know.

Configuration info:

<bean id="sample_dao" class="com.foo.SampleDaoImpl">
  <property name="dataSource" ref="sample_data_source"/>
  ..
  <property name="sampleProperty" ref="sample_bean"/>
</bean>

   <bean id="sample_data_source" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" depends-on="some_sample">
  <property name="driverClass" value="${driverClassName}"/>
  <property name="jdbcUrl" value="${url}"/>
  <property name="user" value="${username}"/>
  <property name="password" value="${password}"/>
  <property name="initialPoolSize" value="1"/>
  <property name="maxPoolSize" value="4"/>
  <property name="acquireIncrement" value="1"/>
  <property name="preferredTestQuery" value="select 1 from sample_table"/>
  <property name="idleConnectionTestPeriod" value="30"/>
  <property name="testConnectionOnCheckin" value="true"/>
  <property name="acquireRetryAttempts" value="3"/>
  <property name="connectionCustomizerClassName" value="com.foo.SampleConnectionCustomizer"/>

+2  A: 

Depending on whether or not you already use AspectJ in your project, you might find the following approach simpler:

Wrap your ComboPooledDataSource instance in a non-final decorator class instance; cglib should be able to proxy the decorator without being any the wiser.

Assuming - for the example's sake - that the dataSource attribute from your SampleDaoImpl class is of type javax.sql.DataSource:

public class ComboPooledDataSourceDecorator implements javax.sql.DataSource {

   private ComboPooledDataSource delegate;

   public void setDelegate(ComboPooledDataSource delegate) {
      this.delegate = delegate;
   }

   public Connection getConnection() {
      return delegate.getConnection();
   }

   // other methods of javax.sql.DataSource

}

Wire it into spring like this:

<bean id="sample_data_source" class="mystuff.ComboPooledDataSourceDecorator">
   <property name="delegate">
      <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
         ...
      </bean>
   </property>
</bean>
kem
This solved my problem - thank you! however, I might go in for AspectJ weaving as there are parts of the application which are still not covered by Spring.
KM