|
»
How is RIFE Database Connection Different From Spring Database Connection?
|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
How is RIFE Database Connection Different From Spring Database Connection?I like the idea of specify parameters (or constraints) so that RIFE
can generate an SQL statement to create a database table. And I have that working with Spring. My Spring configuration file looks like this: <bean id='rifeDatasource' class='com.uwyn.rife.database.Datasource' lazy-init='true'> <property name='driver'><value>org.hsqldb.jdbcDriver</value></property> <property name='url' value='jdbc:hsqldb:file:data/relationalDb'/> <property name='user' value='sa'/> <property name='password' value=''/> </bean> <bean id='createTable' class='com.uwyn.rife.database.queries.CreateTable' lazy-init='true'> <constructor-arg ref='rifeDatasource'/> </bean> <bean id='beerTableCreator' class='org.affy.play.BeerTableCreator' lazy-init='true'> <property name='createTable'><ref bean='createTable'/></property> </bean> I have an abstract class called BaseDatabaseTableCreator which looks like this: abstract public class BaseDatabaseTableCreator implements DatabaseTableCreator { private CreateTable createTable = null; public BaseDatabaseTableCreator() { super(); } abstract public String getSql(); abstract public String getTableName(); abstract public void execute(); public CreateTable getCreateTable() { return this.createTable; } public void setCreateTable(CreateTable _createTable) { this.createTable = _createTable; } } And I have a concrete BeerTableCreator class which looks like: public class BeerTableCreator extends BaseDatabaseTableCreator { public BeerTableCreator() { super(); } public String getSql() { getCreateTable().table(getTableName()) .columns(Beer.class) .primaryKey("id") .precision("brand", 50) .nullable("brand", CreateTable.NOTNULL); return getCreateTable().getSql(); } public void execute() { // NOTE: This is using a RIFE DbConnection. DbConnection dbConnection = getCreateTable().getDatasource().getConnection(); DbStatement dbStatement = dbConnection.createStatement(); dbStatement.execute(getSql()); } public String getTableName() { return "beer"; } } This class might eventually be automatically invoked by the Spring framework, but for now it is not. Instead, I use the following code: public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); DatabaseTableCreator beerTableCreator = (DatabaseTableCreator)ctx.getBean("beerTableCreator"); System.out.println(beerTableCreator.getSql()); beerTableCreator.execute(); } Here's my question. Since I want to actually affect the database (ie, create the table) I want to use Spring's JDBC abstraction layer so all code that affects the database handles exceptions in the same way. My current thinking is that I need to pass *two* data sources into the BeerTableCreator class - a RIFE Datasource and a Spring DataSource. Can anyone suggest another way of doing this? |
|
|
Re: How is RIFE Database Connection Different From Spring Database Connection?Hi David,
RIFE has its own way of handling DB connections, pooling, transactions and database capability compensators. It's totally tied to JDBC, but the fact that RIFE has control of it all means that it can offer a consistent and automated 'no-setup' approach in one jar. Maybe it would be a good approach to implement javax.sql.DataSource in RIFE's Datasource class. I think that this would make it possible for you to reuse it in Spring, no? What do you think? Geert On 2-nov-05, at 18:53, David Medinets wrote: > I like the idea of specify parameters (or constraints) so that RIFE > can generate an SQL statement to create a database table. And I have > that working with Spring. > > My Spring configuration file looks like this: > > <bean id='rifeDatasource' class='com.uwyn.rife.database.Datasource' > lazy-init='true'> > <property name='driver'><value>org.hsqldb.jdbcDriver</value></ > property> > <property name='url' value='jdbc:hsqldb:file:data/relationalDb'/> > <property name='user' value='sa'/> > <property name='password' value=''/> > </bean> > > <bean id='createTable' > class='com.uwyn.rife.database.queries.CreateTable' lazy-init='true'> > <constructor-arg ref='rifeDatasource'/> > </bean> > > <bean id='beerTableCreator' class='org.affy.play.BeerTableCreator' > lazy-init='true'> > <property name='createTable'><ref bean='createTable'/></property> > </bean> > > I have an abstract class called BaseDatabaseTableCreator which > looks like this: > > abstract public class BaseDatabaseTableCreator implements > DatabaseTableCreator { > > private CreateTable createTable = null; > > public BaseDatabaseTableCreator() { > super(); > } > > abstract public String getSql(); > > abstract public String getTableName(); > > abstract public void execute(); > > public CreateTable getCreateTable() { > return this.createTable; > } > > public void setCreateTable(CreateTable _createTable) { > this.createTable = _createTable; > } > > } > > And I have a concrete BeerTableCreator class which looks like: > > public class BeerTableCreator extends BaseDatabaseTableCreator { > > public BeerTableCreator() { > super(); > } > > public String getSql() { > getCreateTable().table(getTableName()) > .columns(Beer.class) > .primaryKey("id") > .precision("brand", 50) > .nullable("brand", CreateTable.NOTNULL); > > return getCreateTable().getSql(); > } > > public void execute() { > // NOTE: This is using a RIFE DbConnection. > DbConnection dbConnection = > getCreateTable().getDatasource().getConnection(); > DbStatement dbStatement = dbConnection.createStatement(); > dbStatement.execute(getSql()); > } > > public String getTableName() { > return "beer"; > } > > } > > This class might eventually be automatically invoked by the Spring > framework, but for now it is not. Instead, I use the following code: > > public static void main(String[] args) { > ApplicationContext ctx = new > ClassPathXmlApplicationContext("spring.xml"); > > DatabaseTableCreator beerTableCreator = > (DatabaseTableCreator)ctx.getBean("beerTableCreator"); > System.out.println(beerTableCreator.getSql()); > beerTableCreator.execute(); > } > > Here's my question. Since I want to actually affect the database (ie, > create the table) I want to use Spring's JDBC abstraction layer so all > code that affects the database handles exceptions in the same way. > > My current thinking is that I need to pass *two* data sources into the > BeerTableCreator class - a RIFE Datasource and a Spring DataSource. > > Can anyone suggest another way of doing this? > _______________________________________________ > Rife-devel mailing list > Rife-devel@... > http://www.uwyn.com/mailman/listinfo/rife-devel > -- Geert Bevin Uwyn bvba "Use what you need" Avenue de Scailmont 34 http://www.uwyn.com 7170 Manage, Belgium gbevin[remove] at uwyn dot com Tel +32 64 84 80 03 PGP Fingerprint : 4E21 6399 CD9E A384 6619 719A C8F4 D40D 309F D6A9 Public PGP key : available at servers pgp.mit.edu, wwwkeys.pgp.net |
|
|
Re: How is RIFE Database Connection Different From Spring Database Connection?On 11/2/05, Geert Bevin <gbevin@...> wrote:
> Maybe it would be a good approach to implement javax.sql.DataSource > in RIFE's Datasource class. I think that this would make it possible > for you to reuse it in Spring, no? I'd like to be able to inject a DataSource into the RIFE Datasource class. For example, I use the following DataSource in my project: <bean id='dataSource' class='org.apache.commons.dbcp.BasicDataSource' lazy-init='true'> <property name='driverClassName'><value>org.hsqldb.jdbcDriver</value></property> <property name='url' value='jdbc:hsqldb:file:data/relationalDb'/> <property name='username' value='sa'/> <property name='password' value=''/> </bean> It would be wonderful if I could define the rifeDatasource bean like so: <bean id='rifeDatasource' class='com.uwyn.rife.database.Datasource' lazy-init='true'> <constructor-arg ref='dataSource'/> </bean> I haven't studied the code deeply, but I'd be happy to take a stab are implementing such a constructor. I see the following steps: - create or identify unit tests for all exceptions and error conditions of Datasource. - create Datasource constructor which accepts DataSource object. - twiddle the code so that the connection pool of the DataSource object is used if it is available. Am I missing anything? Should I create a ticket before taking a shot at code modification or is using this mailing list to communication okay? -david |
|
|
Re: How is RIFE Database Connection Different From Spring Database Connection?Hi David,
I initially thought of this approach too, the problem is that the javax.util.DataSource interface doesn't provide the JDBC driver name through any of the methods. This is of critical importance for RIFE since the whole database abstraction is based on this. Also, plugging in another connection pool will not be trivial since that's where the RIFE JDBC wrapping layer starts. It is closely tied to the DbConnection and Datasource. This is were parts of it ease of use come from. I think that this approach would be the nicest, but sadly I don't think it's feasible. We can discuss things freely here, but once some work starts I like to create an issue in JIRA. That makes it easy to plan releases and the progress on outstanding issues. Best regards, Geert On 2-nov-05, at 19:40, David Medinets wrote: > On 11/2/05, Geert Bevin <gbevin@...> wrote: >> Maybe it would be a good approach to implement javax.sql.DataSource >> in RIFE's Datasource class. I think that this would make it possible >> for you to reuse it in Spring, no? > > I'd like to be able to inject a DataSource into the RIFE Datasource > class. For example, I use the following DataSource in my project: > > <bean id='dataSource' > class='org.apache.commons.dbcp.BasicDataSource' lazy-init='true'> > <property name='driverClassName'><value>org.hsqldb.jdbcDriver</ > value></property> > <property name='url' value='jdbc:hsqldb:file:data/relationalDb'/> > <property name='username' value='sa'/> > <property name='password' value=''/> > </bean> > > It would be wonderful if I could define the rifeDatasource bean > like so: > > <bean id='rifeDatasource' class='com.uwyn.rife.database.Datasource' > lazy-init='true'> > <constructor-arg ref='dataSource'/> > </bean> > > I haven't studied the code deeply, but I'd be happy to take a stab are > implementing such a constructor. I see the following steps: > > - create or identify unit tests for all exceptions and error > conditions of Datasource. > - create Datasource constructor which accepts DataSource object. > - twiddle the code so that the connection pool of the DataSource > object is used if it is available. > > Am I missing anything? Should I create a ticket before taking a shot > at code modification or is using this mailing list to communication > okay? > > -david > _______________________________________________ > Rife-devel mailing list > Rife-devel@... > http://www.uwyn.com/mailman/listinfo/rife-devel > -- Geert Bevin Uwyn bvba "Use what you need" Avenue de Scailmont 34 http://www.uwyn.com 7170 Manage, Belgium gbevin[remove] at uwyn dot com Tel +32 64 84 80 03 PGP Fingerprint : 4E21 6399 CD9E A384 6619 719A C8F4 D40D 309F D6A9 Public PGP key : available at servers pgp.mit.edu, wwwkeys.pgp.net |
| Free embeddable forum powered by Nabble | Forum Help |
