I like using JNDI as that's a standard way to obtain DataSource in J2EE.
If you'd still want to preserve the property files (which is fine too
of course), here is one possible solution - defer the real DataSource
creation with a custom org.objectstyle.cayenne.conf.DataSourceFactory
(can be set per DataNode in the Modeler):
// DataSource that does nothing
class NoopDataSource implements DataSource {
...
}
public class NoopDataSourceFactory implements DataSourceFactory {
public DataSource getDataSource(String location) {
return new NoopDataSource();
}
}
That'll get you started, i.e. your initialization code below will
work after the call to 'getSharedConfiguration'. But you can go
further and implement a smarter DataSourceFactory of course (such as
one actually reading the properties file inside 'getDataSource').
Andrus
On Jan 16, 2006, at 4:54 PM, Dov Rosenberg wrote:
> We are trying to read and set our configuration information from an
> existing properties file. Below is the relevant code. We wrote an
> initialization Servlet with an init() method to do the
> initialization. Our cayenne deployment files do not contain any
> connection info. We want to deploy the app without any db connect
> info.
>
>
>
> Code:
> public static void init() {
> System.out.println("Initializing Database connections");
> try {
> String driver = IMWSConfiguration.getPropertyForKey("JDBCDriver");
> String url = IMWSConfiguration.getPropertyForKey("JDBCURL");
> String user = IMWSConfiguration.getPropertyForKey("JDBCUserid");
> String pass = IMWSConfiguration.getPropertyForKey("JDBCPassword");
> DataSource dataSource = new PoolManager(driver, url, 1, 5, user,
> pass);
>
> System.out.println("1");
> Configuration config = Configuration.getSharedConfiguration();
> System.out.println("2");
> DataDomain domain = config.getDomain();
> System.out.println("3");
> DataNode node = domain.getNode("IMDataNode");
> System.out.println("4");
> node.setDataSource(dataSource);
> System.out.println("5");
>
> System.out.println("Database OK");
> } catch (SQLException e) {
> e.printStackTrace();
> logger.error("Can't connect to database");
> System.out.println("Can't connect to database");
> logger.error(e);
>
> throw new IMDataException("Can't Connect to Database");
> }
>
> }
>
>
> Everything works except during the initialization the
> Configuration.getSharedConfiguration(); method generates the
> following output:
>
> Initializing Database connections
> 1
> - started configuration loading.
> - loaded domain: UntitledDomain
> - loaded <map name='Content' location='Content.map.xml'>.
> - loading <node name='IMDataNode'
> datasource='IMDataNode.driver.xml'
> factory='org.objectstyle.cayenne.conf.DriverDataSourceFactory'>.
> - using factory: org.objectstyle.cayenne.conf.DriverDataSourceFactory
> - loading driver information from 'IMDataNode.driver.xml'.
> - loading driver null
> - Created connection pool: null
> Driver class: null
> Min. connections in the pool: 1
> Max. connections in the pool: 1
> - loaded datasource.
> - no adapter set, using automatic adapter.
> - loaded map-ref: Content.
> - finished configuration loading in 629 ms.
> 2
>
> Our information gets set after the getSharedConfiguration() call
> and is correct, but the log output happens before our info is set.
> In the EOF world we listened for a ModelLoaded notification and
> intercepted it to perform our Connection Dictionary replacement.
> That worked great. Is there something similar in Cayenne?
>
>
This archive was generated by hypermail 2.0.0 : Mon Jan 16 2006 - 19:53:11 EST