Skip to content

DATAMONGO-181: Ensure Mongo is closed when MongoFactoryBean is destroyed #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
Expand All @@ -36,7 +37,7 @@
* @author Oliver Gierke
* @since 1.0
*/
public class MongoFactoryBean implements FactoryBean<Mongo>, PersistenceExceptionTranslator {
public class MongoFactoryBean implements FactoryBean<Mongo>, DisposableBean, PersistenceExceptionTranslator {

private MongoOptions mongoOptions;
private String host;
Expand All @@ -47,6 +48,8 @@ public class MongoFactoryBean implements FactoryBean<Mongo>, PersistenceExceptio

private PersistenceExceptionTranslator exceptionTranslator = new MongoExceptionTranslator();

private Mongo mongo;

public void setMongoOptions(MongoOptions mongoOptions) {
this.mongoOptions = mongoOptions;
}
Expand Down Expand Up @@ -82,8 +85,6 @@ public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTrans

public Mongo getObject() throws Exception {

Mongo mongo;

ServerAddress defaultOptions = new ServerAddress();

if (mongoOptions == null) {
Expand Down Expand Up @@ -127,6 +128,12 @@ public boolean isSingleton() {
return true;
}

public void destroy() throws Exception {
if (this.mongo != null) {
this.mongo.close();
}
}

/*
* (non-Javadoc)
* @see org.springframework.dao.support.PersistenceExceptionTranslator#translateExceptionIfPossible(java.lang.RuntimeException)
Expand Down