Using Cloudscape as a J2EE Resource Manager,
Page 3 of 3


[top]
[prev]
[next]
Documentation Top
Global Index
Reference Manual
TOC Index
Grammar index
Developer's Guide
TOC Index
Tuning Cloudscape
TOC Index

How to Use Them

Getting a DataSource

Normally, you can simply work with the interfaces for javax.sql.DataSource, javax.sql.ConnectionPoolDataSource, and javax.sql.XADataSource, as shown in the following examples. However, you do need to use the Cloudscape class COM.cloudscape.core.DataSourceFactory.

import COM.cloudscape.core.DataSourceFactory;
javax.sql.ConnectionPoolDataSource cpdf = DataSourceFactory.getConnectionPoolDataSource()
javax.sql.DataSource ds = DataSourceFactory.getDataSource();
javax.sql.XADataSource xads = DataSourceFactory.getXADataSource();

Cloudscape provides three methods for a DataSource that are not part of the standard API. These methods are in COM.cloudscape.core.AbstractDataSource. They are:

  • setCreateDatabase(String databaseName)
    Sets a property to create a database the next time the XADataSource.getXAConection() method is called. databaseName can be a simple name or an absolute path to the database and is equivalent to the databaseName in the Cloudscape database connection URL.
  • setShutdownDatabase()
    Sets a property to shut down a database. Shuts down the database the next time XADataSource.getXAConnection().getConnection() method is called.
  • setRemoteDataSourceProtocol
    See “Remote DataSources”.

Shutting Down or Creating a Database

If you need to shut down or create a database, it is easiest just to work with the Cloudscape-specific implementations of interfaces, as shown in the following examples.

javax.sql.XADataSource xads = makeXADataSource(mydb, true);



// example of setting property directory using Cloudscape’s
// XaDataSource object
import COM.cloudscape.core.DataSourceFactory;
import COM.cloudscape.core.XaDataSource;
import javax.sql.XADataSource;
// dbname is the database name
// if create is true, create the database if not already created
XADataSource makeXADataSource (String dbname, boolean create) throws Throwable
{
    XaDataSource xads = DataSourceFactory.getXADataSource();
    // use Cloudscape’s setDatabaseName call
    xads.setDatabaseName(dbname);
    if (create)
        xads.setCreateDatabase("create");
    return xads;
}

Or, you could use reflection:

import javax.sql.DataSource
import COM.cloudscape.core.DataSourceFactory

DataSource ds = makeCloudscapeSource(mydb);


// example of using reflection to set properties
// knowledge of COM.cloudcape.core.DataSourceFactory required
DataSource makeCloudscapeDataSource (String dbname) throws Throwable
{
    Class[] parameter = new Class[1];
    parameter[0] = dbname.getClass();
    DataSource ds = DataSourceFactory.getDataSource();
    Class c1 = ds.getClass();
    
    Method setName = c1.getMethod("setDatabaseName", parameter);
    Object[] arg = new Object[1];
    arg[0] = dbname;
    setName.invoke(ds, arg);

    return ds;
}

You could use similar code to shut down a database.

Setting the property does not create or shut down the database. The database is not actually created or shut down until the next connection request.

Remote DataSources

XaDataSources are local only; no remote support is provided. Remote support is provided for DataSources and LocalConnectionPoolDataSources through RmiJdbc.

To create remote RmiJdbc data sources, use the method setRemoteDataSourceProtocol on the data source before getting the connection. The string argument to the method must be either:

  • “rmi”
  • “rmi://hostname:portnumber/”
[top]
[prev]
[next]


Cloudscape Version 3.0
For technical support, go to: www.cloudscape.com and click Support.
Copyright © 1998 and 1999 Cloudscape, Inc. All rights reserved.