cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adam Ratcliffe" <a...@prema.co.nz>
Subject RE: Using database pool from java object for flow
Date Tue, 29 Mar 2005 19:49:10 GMT
Hi Andre

If you want to access your database from Java then writing an Avalon component
would be a good way to go.

I've included the code for an abstact data access class that I'm using for this
purpose. Additionally the datasource needs to be declared in the datasources
section of cocoon.xconf and the database driver added to web.xml.

Cheers
Adam

package com.geosmart.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.avalon.excalibur.datasource.DataSourceComponent;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.excalibur.pool.Recyclable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.ServiceSelector;
import org.apache.avalon.framework.service.Serviceable;


/**
 * @author Adam Ratcliffe
 */
public class AbstractDB implements Initializable, Disposable, Recyclable,
Serviceable, Poolable, LogEnabled {

    private final String dataSourceName = "lbs";

    protected Logger logger;
    private ServiceManager serviceManager;
    private Connection connection = null;
    private DataSourceComponent datasource;


    /**
     * Returns this component's <code>ServiceManager</code>
     *
     * @return the <code>ServiceManager</code>
     */
    protected ServiceManager getServiceManager() {
        return serviceManager;
    }

    protected Connection getConnection() throws Exception {
        if (connection == null || connection.isClosed()) {
            try {
                connection = datasource.getConnection();
                if (this.logger.isDebugEnabled())
                    this.logger.debug("Connection created: " +
this.connection.hashCode());
            } catch (Exception e) {
                throw new Exception("Could not create SQL connection using
datasource '" + this.dataSourceName + "'!",
                        e);
            }
        }
        return connection;
    }

    /*
     * (non-Javadoc)
     *
     * @see
org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framew
ork.service.ServiceManager)
     */
    public void service(ServiceManager serviceManager) throws ServiceException {
        this.serviceManager = serviceManager;
    }

    /*
     * (non-Javadoc)
     *
     * @see
org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.fr
amework.logger.Logger)
     */
    public void enableLogging(Logger logger) {
        this.logger = logger;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.avalon.framework.activity.Disposable#dispose()
     */
    public void dispose() {
        if (datasource != null) {
            serviceManager.release(datasource);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.avalon.framework.activity.Initializable#initialize()
     */
    public void initialize() throws Exception {
        if (this.dataSourceName == null)
            throw new Exception("No datasource configured yet. Please configure
it!");
        try {
            ServiceSelector selector = (ServiceSelector)
this.serviceManager.lookup(DataSourceComponent.ROLE
                    + "Selector");
            this.datasource = (DataSourceComponent)
selector.select(this.dataSourceName);
            serviceManager.release(selector);
        } catch (ServiceException e) {

        }
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
     */
    public void recycle() {
        close(connection);
        connection = null;
    }

    protected void close(java.sql.Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            if (this.logger.isErrorEnabled()) {
                this.logger.debug("Error closing Connection - SQLException:  " +
e.getMessage());
            }
        }
    }

}



> -----Original Message-----
> From: Andre Thenot [mailto:andre-lists@thenot.org]
> Sent: Wednesday, 30 March 2005 3:00 a.m.
> To: users@cocoon.apache.org
> Subject: Using database pool from java object for flow
>
>
> Hi,
>
> I'm using flow to talk to my java model classes. So far so good.
>    But I'd like my model classes to talk to the database and use
> cocoon's connection pool. Do I have to write an Avalon component for
> this? If so, how does one go about to do this (I don't know anything
> about writing those)?
>
> Any pointers would be greatly appreciated.
> Thanks,
>
> Andre.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message