You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Adam Jenkins <ma...@adamjenkins.net> on 2005/05/04 11:37:48 UTC

how to clear a jdbc connection descriptor and associated connections?

Hi All,

I have a test case where, in the setUp method, I create a hypersonic
database (random dbalias), run the table set up scripts, run my tests
(using dbunit) then, in the tearDown method I swap out the descriptor
and delete the database in preparation for the next test.

I'm having a problem where the JdbcConnectionDescriptor is successfully
replaced (I can see the one to the new dbalias being loaded), however
when I apply the scripts it tries to apply to the previous database (I'm
guessing it's pulling a connection from the pool that is still
associated with the old database?)...both JdbcConnectionDescriptor
objects are associated with the same jcdAlias.  Code is below, I would
really appreciate if anyone has any thoughts?

Cheers
Adam


    public synchronized void setUp() throws Exception{
        log.debug("Creating descriptor.");
        String tmpDir = System.getProperty("test.tmp.dir");
        if(tmpDir == null) tmpDir = System.getProperty("tmp.dir");
        if(tmpDir == null) {
            tmpDir = System.getProperty("user.home") + File.separator +
".testing";
            final File tmpDirFile = new File(tmpDir);
            if(!tmpDirFile.exists()) tmpDirFile.mkdir();
        }
        descriptor = new JdbcConnectionDescriptor();
        descriptor.setDefaultConnection(false);
        descriptor.setJcdAlias("certificate-manager");
        descriptor.setDbms("hsqldb");
        descriptor.setJdbcLevel(2.0);
        descriptor.setDriver("org.hsqldb.jdbcDriver");
        descriptor.setProtocol("jdbc");
        descriptor.setSubProtocol("hsqldb");
        filePrefix = tmpDir + File.separator + "test" + rnd.nextInt();
        log.debug("Test Database Prefix and Loc: " + filePrefix);
        descriptor.setDbAlias("file:" + filePrefix);
        descriptor.setUserName("sa");
        descriptor.setPassWord("");

MetadataManager.getInstance().connectionRepository().addDescriptor(descriptor);
        final IDatabaseConnection connection = getConnection();
        final InputStream in =
Thread.currentThread().getContextClassLoader().getResourceAsStream(

"/com/infocomp/ssl/certificatemanager/scripts/hsqldb/database-schema.sql"
        );
        log.debug("Script input stream: " + in);
        applyScript(
                IOUtils.toString(in),
                connection.getConnection()
        );
    }



    public synchronized void tearDown(){

MetadataManager.getInstance().connectionRepository().removeDescriptor(descriptor);
        releaseAllInstances();
        if(log.isDebugEnabled()){
                log.debug("Inspecting connection repository.");
                final ConnectionRepository repository =
MetadataManager.getInstance().connectionRepository();
                for(JdbcConnectionDescriptor descriptor :
(List<JdbcConnectionDescriptor>)repository.getAllDescriptor()){
                    log.debug("Active Descriptor: " +
descriptor.getJcdAlias() + " is connected to " +
descriptor.getDbAlias());
                }
        }
        final File logFile = new File(filePrefix + ".log");
        final File propertiesFile = new File(filePrefix +
".properties");
        logFile.delete();
        propertiesFile.delete();
    }



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: how to clear a jdbc connection descriptor and associated connections?

Posted by Martin Kalén <mk...@apache.org>.
Adam Jenkins wrote:
> I have a test case where, in the setUp method, I create a hypersonic
> database (random dbalias), run the table set up scripts, run my tests
> (using dbunit) then, in the tearDown method I swap out the descriptor
> and delete the database in preparation for the next test.
> 
> I'm having a problem where the JdbcConnectionDescriptor is successfully
> replaced (I can see the one to the new dbalias being loaded), however
> when I apply the scripts it tries to apply to the previous database (I'm
> guessing it's pulling a connection from the pool that is still
> associated with the old database?)...both JdbcConnectionDescriptor
> objects are associated with the same jcdAlias.

You are hitting a problem with the way Connection pools are beeing cached
in memory in OJB 1.0.x (this has been slightly refactored in OJB 1.1).

In order to clear the connection factory completely, you have to do
something like this (hack warning, ugly cast follows):

  ConnectionManagerIF cman = broker.serviceConnectionManager();
  broker.close();
  ConnectionFactory cf = ((ConnectionManagerImpl) cman).getUnderlyingConnectionFactory();
  cf.releaseAllResources();

I assume that you use the default ConnectionFactoryPooledImpl.

In your tests, you can also try switching to ConnectionFactoryNotPooledImpl,
giving you less troubles with cached Java-references in memory but poor
performance since every Connection object is created and destroyed
for every pool operation.

HTH,
  Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org