cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <>
Subject Re: model model, who's got the model….
Date Wed, 16 May 2012 18:24:50 GMT
I've done this for years.

      <description>file name of cayenne.xml - leave blank for


There was a lot of extra stuff in here you don't probably need since
this application not only can have different databases, but other
differences in application configuration, but this sanitized file
should give you the starting point you need.

All of the various cayenne.xml files you might want to support would
be stored in the com/xyz/cayenne/model/app package although you can
change that.   Each cayenne.xml file points to the same <map> but has
different database connectivity defined in the <node> definitions
[datasource, adapter,  factory].

Note that this is a Cayenne 1.1 project so you may have to update some
code to use it with more modern versions.


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.objectstyle.cayenne.access.DataDomain;
import org.objectstyle.cayenne.access.DataNode;
import org.objectstyle.cayenne.access.QueryLogger;
import org.objectstyle.cayenne.conf.Configuration;
import org.objectstyle.cayenne.conf.DefaultConfiguration;
import org.objectstyle.cayenne.conf.WebApplicationListener;

 * Special subclass of ServletConfiguration that enables
 * logging of Cayenne queries and can also perform some
 * custom tasks on servlet container startup.
public class CustomConfiguration extends WebApplicationListener
    private static final String CAYENNE_DOMAIN_FILE_NAME =

    protected static Log log = LogFactory.getLog(CustomConfiguration.class);

	public CustomConfiguration()
        if (log.isTraceEnabled())  log.trace("in CustomConfiguration()");


	protected void configureLogging() {
		// debug configuration
		// Configuration.setLoggingLevel(Level.ALL);

    /** Establishes a Cayenne shared Configuration object that can
later be obtained by calling
      * <code>Configuration.getSharedConfiguration()</code>.
      * This method is a part of ServletContextListener interface and is called
      * on application startup. */
    public void contextInitialized(ServletContextEvent sce) {
        if (log.isTraceEnabled())  log.trace("in


            DataDomain dataDomain = getConfiguration().getDomain();

            // Ignore errors generated by the driver id logging
                Iterator dataNodeIterator =
                while (dataNodeIterator.hasNext())
                    DataNode dataNode = (DataNode);

                    Connection connection = null;
			            connection = dataNode.getDataSource().getConnection();
			            DatabaseMetaData meta = connection.getMetaData ();
			            Logger cayenneLogger = Logger.getLogger(QueryLogger.class);
			            cayenneLogger.debug("Database Product Name is ... " +
			            cayenneLogger.debug("Database Product Version is  " +
			            cayenneLogger.debug("JDBC Driver Name is ........ " +
			            cayenneLogger.debug("JDBC Driver Version is ..... " +
			            cayenneLogger.debug("JDBC Driver Major Version is " +
			            cayenneLogger.debug("JDBC Driver Minor Version is " +
			        catch (SQLException e)
                        catch (SQLException exception)
            catch (RuntimeException e1)
        catch (RuntimeException e)

    /** Currently does nothing. <i>In the future it should close down
      * any database connections if they wheren't obtained via JNDI.</i>
      * This method is a part of ServletContextListener interface and is called
      * on application shutdown. */
    public void contextDestroyed(ServletContextEvent sce) {
        if (log.isTraceEnabled())  log.trace("in


    protected Configuration newConfiguration(ServletContext sc) {
        String domainFileName = sc.getInitParameter(CAYENNE_DOMAIN_FILE_NAME);
        return newConfiguration(domainFileName);

    protected Configuration newConfiguration(String cayenneDomainFileName) {
        DefaultConfiguration conf;
        if (null == cayenneDomainFileName)
            conf = new CustomBasicServletConfiguration();
            conf = new CustomBasicServletConfiguration(cayenneDomainFileName);
        return conf;


On Tue, May 15, 2012 at 5:59 PM, Tony Giaccone <> wrote:
> So here's my situation. I'm building a framework, where I'd like to let my development
team choose to use either sql light or postgres on a shared server as the persistent store.
> Using SQLite  lets them do development locally with out having to install Postgress
on each workstation and or be dependent on a central shared postgres server.
> My first thought was to create two different models and then pick one in the web.xml
file. I'm sure this would work, but the model name would have to be different for each model,
so as to avoid file name collision. I'm not really keen on that idea, because the models are
the same and vary only in the values in the data node.  And then I thought I could put them
in different folders, which represented the datanode file.
> resources/sqlite/myCayenneModel.xml
> resources/postgres/myCayenneModel.xml
> But it's not clear to me, how that is handled in the web.xml file.  The filter name
is the name of the model, no? How do you manage that if the model is not at the top level
of the classpath?
> After thinking about it, it seems to me the best solution is to ask you all.
> Just to be pedantic here's the problem.
> I have two different data models which essentially different only in the value of the
 data node.
> I want to be able to choose one model at compile time.
> I don't want to have to change any java code to make the choice. I'd prefer to just edit
the web.xml file.
> Anyone have a solution?
> Tony Giaccone

View raw message