Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resource not available in @Singleton @Predestroy method

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.

like image 237
Kiran Mohan Avatar asked Dec 21 '15 15:12

Kiran Mohan


People also ask

What is the PreDestroy method annotated with?

The method annotated with PreDestroy is typically used to release resources that it has been holding. This annotation MUST be supported by all container managed objects that support PostConstruct except the application client container in Java EE 5.

What are @PostConstruct and @PreDestroy annotations in Spring Boot?

@PostConstruct and @PreDestroy and important annotations to use with the spring bean lifecycle management. We can use them to verify that bean is properly initialized and then close all the resources when the bean is removed from the spring context. You can check out the complete project code from our GitHub Repository.

Which Container Managed objects are supported by the PreDestroy annotation?

This annotation MUST be supported by all container managed objects that support PostConstruct except the application client container in Java EE 5. The method on which the PreDestroy annotation is applied MUST fulfill all of the following criteria:

What happens if a PreDestroy method returns a value?

If a PreDestroy interceptor method returns a value, it is ignored by the container. The method on which PreDestroy is applied MAY be public, protected, package private or private. The method MUST NOT be static.


Video Answer


1 Answers

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.

like image 124
snovelli Avatar answered Oct 28 '22 19:10

snovelli