Resource not available in @Singleton @Predestroy method
Asked Answered
U

1

6

It seems like resources are not available to a Singleton's @Predestroy method.

@PreDestroy
public void cleanup() {
    logger.info("*** Application shutting down. Dropping temporary tables ***");
    try {
        connection = dataSource.getConnection();

        Statement statement = connection.createStatement();
        statement.execute("drop table TABLE1");
        statement.execute("drop table TABLE2");
        connection.close();
        connection = null;
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}

The call to getConnection() fails with the error "No Pool Meta Data object associated with the pool". Note that the getConnection() call is successful in the @PostConstruct methods.

Is it a Bug in the application server implementation? If not, what is the most elegant way to drop temporary tables?

(Using Glassfish 4.1.1 + Derby DB. The datasource is created using glassfish-resources.xml deployed with the EAR

<resources>
    <jdbc-resource pool-name="EmbeddedDerbyPool"
                   jndi-name="java:app/jdbc/ActionBazaarDS" />
    <jdbc-connection-pool name="EmbeddedDerbyPool"
                          res-type="javax.sql.DataSource"
                          datasource-classname="org.apache.derby.jdbc.EmbeddedDataSource"
                          is-isolation-level-guaranteed="false">
        <property name="databaseName" value="memory:action-bazaar-db"/>
        <property name="createDatabase" value="create"/>
    </jdbc-connection-pool>
</resources>

)

Update:
I created a bug report in GlassFish https://java.net/jira/browse/GLASSFISH-21476.

Unreliable answered 21/12, 2015 at 15:44 Comment(0)
I
2

As this is a @Singleton, the @PreDestroy is presumably only called on context shutdown, therefore is possible that dataSource is already been destroyed/finalized/closed (can't be specific here as I don't know the type of datasource) therefore the error.

Intwine answered 21/12, 2015 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.