Commit 81864fbe authored by santiagoperaza93's avatar santiagoperaza93

Conexion a MongoDB con Singleton

parent 6f84deda
......@@ -35,10 +35,7 @@ import logica.MongoClientProvider;
@LocalBean
public class CategoriaEJB implements CategoriaEJBLocal {
@Inject
MongoClientProvider mongoClientProvider;
MongoClient mongoClientNube = mongoClientProvider.getMongoClient();
MongoClient mongoClientNube = MongoClientProvider.CONNECTION.getMongoClient();
//MongoClient mongoClientNube = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
// MongoClient mongoClientNube = MongoClients.create(
......
......@@ -30,10 +30,7 @@ import logica.MongoClientProvider;
@LocalBean
public class NotificacionEJB implements NotificacionEJBLocal {
@Inject
MongoClientProvider mongoClientProvider;
MongoClient mongoClientNube = mongoClientProvider.getMongoClient();
MongoClient mongoClientNube = MongoClientProvider.CONNECTION.getMongoClient();
//MongoClient mongoClientNube = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
// MongoClient mongoClientNube = MongoClients.create(
......
......@@ -49,10 +49,7 @@ public class PartidaEJB implements PartidaEJBLocal {
@EJB
PreguntasEJB preguntas;
@Inject
MongoClientProvider mongoClientProvider;
MongoClient mongoClientNube = mongoClientProvider.getMongoClient();
MongoClient mongoClientNube = MongoClientProvider.CONNECTION.getMongoClient();
// MongoClient mongoClientNube = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
// MongoClient mongoClientNube = MongoClients.create(
......
......@@ -38,10 +38,7 @@ public class PreguntasEJB implements PreguntasEJBLocal{
@EJB
private CategoriaEJB categorias;
@Inject
MongoClientProvider mongoClientProvider;
MongoClient mongoClientNube = mongoClientProvider.getMongoClient();
MongoClient mongoClientNube = MongoClientProvider.CONNECTION.getMongoClient();
//MongoClient mongoClientNube = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
// MongoClient mongoClientNube = MongoClients.create(
......
package logica;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class MongoClientProvider {
private MongoClient mongoClient = null;
@Lock(LockType.READ)
public MongoClient getMongoClient(){
return mongoClient;
}
@PostConstruct
public void init() {
mongoClient = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
}
public enum MongoClientProvider{
CONNECTION;
private MongoClient client = null;
private MongoClientProvider() {
try {
client = new MongoClient(new MongoClientURI("mongodb://proyecto:proyecto.20@mongodb/proyectoBDMongo"));
} catch (Exception e) {
// Log it.
}
}
public MongoClient getMongoClient() {
if (client == null)
throw new RuntimeException();
return client;
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment