Cayenne User Documentation
Standalone Applications

### Shared Configuration Singleton

In a standalone Java application, DefaultConfiguration is used to locate and load configuration files. This approach does not require any additional setup. Shared configuration instance can simply be obtained by calling `getSharedConfiguration()`:

    import org.apache.cayenne.conf.Configuration;
    ...
    Configuration conf = Configuration.getSharedConfiguration();

DefaultConfiguration will expect cayenne.xml file to be located in the CLASSPATH. The same is true for DataMaps referenced in cayenne.xml. Their location is resolved relative to CLASSPATH as well. To make these files available to Cayenne, you can simply include them in the root of your application jar file (read Customizing Configuration on how to change that).

The location of the data source files referenced in cayenne.xml is interpreted by the factory that was assigned to each datasource in cayenne.xml. Default factory is DriverDataSourceFactory. It will rely on its parent configuration to find the data source file (i.e. locating data source files will be no different from the DataMap files). Depending on the application needs, programmers may implement custom factories that collect database information interactively by showing a login dialog, etc.

### Shared Configuration and DataContext

In a setup that relies on shared Configuration a DataContext can be created by calling `DataContext.createDataContext()` static method that internally uses shared configuration:

    import org.apache.cayenne.access.DataContext;
    ...
    DataContext context = DataContext.createDataContext();

Later a DataContext can be passed around in the code explicitly, or it can be bound to an execution thread, making it accessible to all code being run within this thread (e.g. this can be a Swing event thread):

    import org.apache.cayenne.access.DataContext;
    ...
    DataContext context = DataContext.createDataContext();
    DataContext.bindThreadDataContext(context);

.
Copyright ©2001-2008 Apache Software Foundation