incubator-empire-db-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eike Kettner <n...@eknet.org>
Subject Re: Spring Configuration
Date Thu, 28 Jan 2010 23:14:40 GMT

Hi there

As it happens that i like to use spring :), I might contribute to this
with something that works for me so far... 

as empire-db sits on top of JDBC, all I do is extending Springs
JDBCDaoSupport (calling it EmpireDaoSupport) and adding some setters
for the DBDatabase and DBDriver class. In the application context I
setup the DBDatabaseDriver and my DBDatabase and have them injected in
any Dao that extends the EmpireDaoSupport class. I have just played
with it using the BasicDatasource and a DatasourceTransactionManager
provided by commons-dbcp and spring. The database driver class and the
JDBC Settings are injected using normal placeholders from some
configuration file (xml or whatever you like, if it is a
java.util.Property at the end) - like you'd do it with spring.

more concrete, my spring applicationXyz.xml:
<xml>
...
<!-- Data Source / DB Settings -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
     <property name="url" value="${jdbc.url}" />
     <property name="username" value="${jdbc.username}" />
     <property name="password" value="${jdbc.password}" />
</bean>

<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg ref="dataSource" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="databaseDriver" class="${empiredb.drivername}">
   <property name="databaseName" value="${empiredb.databasename}" />
</bean>
<!-- this is my DBDatabase subclass -->
<bean id="ebxDatabase" class="org.eknet.ebx.db.EbxDatabase">
</bean>

<bean id="userDao" class="org.eknet.ebx.impl.UserDaoImpl">
    <property name="dataSource" ref="dataSource" />
    <property name="database" ref="ebxDatabase" />
    <property name="databaseDriver" ref="databaseDriver" />
</bean>


And this is the EmpireDaoSupport class:

public class EmpiredbDaoSupport extends JdbcDaoSupport {

  private DBDatabase db;
  private DBDatabaseDriver dbdriver;

  public EmpiredbDaoSupport() {
  }

  public void setDatabase(DBDatabase db) {
    this.db = db;
  }

  public void setDatabaseDriver(DBDatabaseDriver dbdriver) {
   this.dbdriver = dbdriver;
  }

  @Override
  protected void checkDaoConfig() {
    super.checkDaoConfig();
    if (db == null)
      throw new IllegalArgumentException("'database' must be given!");
    if (dbdriver == null)
      throw new IllegalArgumentException("'databaseDriver' must be given!");
  }

 public DBDatabase getDatabase() {
   if (!db.isOpen())
      db.open(dbdriver, getConnection());
      return db;
  }

  @SuppressWarnings("unchecked")
  public <T extends DBDatabase> T getDB() {
     return (T) getDatabase();
  }

  public DBDatabaseDriver getDatabasedriver() {
    return dbdriver;
  }

  public RuntimeException translateEmpireException(EmpireException e) {
    return new DataAccessResourceFailureException(e.getLocalizedMessage());
  }

  protected DBReader openReader(DBCommand cmd, Connection conn) {
     DBReader r = new DBReader();
     r.open(cmd, conn);
     return r;
  }
        
}

Then implementing any DAO object is the same like with jdbc or
hibernate - extend the support class and implement it with empire-db:

public class UserDaoImpl extends EmpiredbDaoSupport implements UserDao
{

    @Override
    public int findAllUserCount() {
        EbxDatabase db = getDB();
        Connection conn = getConnection();

        UserTable ut = db.TBL_USER;
        DBCommand cmd = db.createCommand();
        cmd.select(ut.count());
    ...

}

bye, eike

On [Mon, 04.01.2010 14:23], Jaco van Tonder wrote:
> Hello guys,
> 
> I am busy investigating Empire-DB as a replacement for Hibernate in our organization
(so far so good). :) I have a couple of questions though.
> 
> I put together a config class that does not make use of a config file (looked at the
examples, and just used the defaults), but instead is wired using Spring (simple setter injection).
I could not find any examples that uses this method so I am not sure if this is the best way
to do it. I looked at the source code and could not find any other way of configuring Empire-DB
that is not XML based.
> 
> Q: Is there a recommended way of configuring Empire-DB using Spring?
> 
> I read that there is no built-in transaction support. Obviously this is important for
databases. :)
> 
> Q: What transaction managers are known to work well with Empire-DB? JOTM, etc etc etc?
Are there any examples of this available somewhere?
> 
> I am willing to document any of the answers (ie spring configuration, transaction manager
examples) on the project wiki.
> 
> Thank you in advance.
> 
> ---Jaco
> 
> 
> #####################################################################################
> Attention:
> The information contained in this message and or attachments is intended
> only for the person or entity to which it is addressed and may contain
> confidential and/or privileged material.  Any review, retransmission,
> dissemination or other use of, or taking of any action in reliance upon,
> this information by persons or entities other than the intended recipient
> is prohibited. If you received this in error, please contact the sender and
> delete the material from any system and destroy any copies.
> 
> Thank You.
> #####################################################################################
> 
> #####################################################################################
> This e-mail message has been scanned for Viruses and Content and cleared 
> by MailMarshal
> #####################################################################################

-- 
email: eike@eknet.org   https://www.eknet.org  pgp: 481161A0

Mime
View raw message