|
From: Ken K. <kk...@kk...> - 2003-06-03 15:28:09
|
On Friday, I was working on an HSQL version of the incrementer but had
not completed the work before leaving for an extended holiday. I was
intending to use it so as to provide support in the Petclinic demo for
both Mysql and Hsql. I was pleased to find it done when I got back :=).
I have built the demo using both db's now and the insert use case works
well on both versions.
I would like to propose a bit of refactoring to the incrementers class
hierarchy to enhance pluggability. As it stands now, there is a lot of
common functionality that is redundant in the concrete classes and could
be pushed up to the AbstractDataFieldMaxValueIncrementer class:
1. cacheSize field and setter
2. prefixWithZero field and setter
3. dataSource field and setter
4. sequenceName (Oracle) or tableName (Mysql and Hsql) field and setter,
perhaps renamed to incrementerName
NOTE: The interface for RdmsMaxValueIncrementer is inconsistent with the
others and out-of-date. As it is also unsafe, perhaps it should be
removed altogether. It should at least be brought up-to-date.
I have provided an example to show how these changes can benefit the
user (developer):
In Petclinic, I provide beans in applicationContext.xml to configure the
dataSource and incrementer for my DAO bean. At present, I only have 1
insert use case, addVisit.
NOTE: I have elected to show the use of pooled datasources in a later
iteration of the demo.
For mysql:
<bean name="dataSource"
class="com.interface21.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://localhost:3306/petclinic</property>
<property name="username">pc</property>
<property name="password">pc</property>
</bean>
<bean name="visitIncrementer"
class="com.interface21.jdbc.core.support.MySQLMaxValueIncrementer" >
<property name="dataSource" beanRef="true">dataSource</property>
<property name="tableName">visits_seq</property>
<property name="columnName">seq</property>
</bean>
For hsql:
<bean name="dataSource"
class="com.interface21.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">org.hsqldb.jdbcDriver</property>
<property name="url">jdbc:hsqldb:hsql://localhost:9001</property>
<property name="username">sa</property>
</bean>
<bean name="visitIncrementer"
class="com.interface21.jdbc.core.support.HsqlMaxValueIncrementer" >
<property name="dataSource" beanRef="true">dataSource</property>
<property name="tableName">visits_seq</property>
</bean>
My DAO bean:
<bean name="clinicDAO" class="petclinic.dao.ClinicJdbcDAO" >
<property name="dataSource" beanRef="true">dataSource</property>
<property name="visitIncrementer"
beanRef="true">visitIncrementer</property>
</bean>
As it stands, I need to configure a separate incrementer bean for each
new insert use case and add a field and setter for each one to the DAO
or alternatively embed code using the specific concrete class names in
the DAO which is now generic JDBC.
What I would like to do is something like this:
<bean name="incrementer"
singleton="false"
class="com.interface21.jdbc.core.support.MySQLMaxValueIncrementer" >
<!-- or some other concrete incrementer -->
<property name="dataSource" beanRef="true">dataSource</property>
</bean>
<bean name="clinicDAO" class="petclinic.dao.ClinicJdbcDAO" >
<property name="dataSource" beanRef="true">dataSource</property>
<property name="incrementer" beanRef="true">incrementer</property>
</bean>
I could then use this as a prototype bean for each of the insert use
cases which would then instantiate a prototype object using getBean() on
an AbstractDataFieldMaxValueIncrementer reference, subsequently setting
the incrementerName property for that object for the particular use case
in code. The code remains completely generic and all the database
configuration/selection is done in xml. A problem remains with the Mysql
version in that the columnName property needs to be set. This could be
accomplished without breaking genericity by conventionally using a
default columnName, such as "seq".
What do you think ???
Ken
|