Using Cloudscape as a J2EE Resource Manager
Page 3 of 3

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 createString)

    Sets a property to create a database the next time the XADataSource.getXAConection() method is called. The string createString is always "create" (or possibly null). (Use the method setDatabaseName() to define the name of the database.)

  • setShutdownDatabase(String shutDownString)

    Sets a property to shut down a database. The string shutDownString is always "shutdown" (or possibly null). 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;
}

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

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/"

Remote support is also provided for XaDataSources through RmiJdbc; use COM.cloudscape.core.RemoteXaDataSource instead of COM.cloudscape.core.XaDataSource.

To create RemoteXaDataSources, use the method setRemoteDataSourceProtocol on it before getting the connection. The string argument to the method must be either:

  • "rmi"
  • "rmi://hostname:portnumber/"

NEW: Support for remote XaDataSources is new in Version 3.6.