Note that there are some explanatory texts on larger screens.

plurals
  1. POMonoDb creates connection every single operation on collection
    primarykey
    data
    text
    <p>I'm using mongodb-java-driver, and nothing else except that. I created singleten EJB with connection to Mongo.</p> <pre><code>@Singleton public class MongoConnection { private DB db = null; private MongoClient mongoClient = null; @PostConstruct public void init() { try { mongoClient = new MongoClient("localhost", 27017); db = mongoClient.getDB("mydb"); } catch (UnknownHostException e) { e.printStackTrace(); } } public MongoConnection() { } public DB getDb() { return db; } public DBCollection getCollectionInDatabase(String collection) { DBCollection coll; coll = db.getCollection(collection); return coll; } } </code></pre> <p>I get this ejb in ApplicationScoped bean (JSF) (just to be sure, that I will have only ONE instance of DB connection). </p> <pre><code>@Named("appMongo") @ApplicationScoped public class MongoApplicationScope implements Serializable{ private static final long serialVersionUID = 1L; @EJB MongoConnection mu; public MongoConnection getMu() { return mu; } public void setMu(MongoConnection mu) { this.mu = mu; } } </code></pre> <p>Then in request scoped bean I get data from db</p> <pre><code>@Named("mongoBean") @SessionScoped public class MongoBean implements Serializable { private static final long serialVersionUID = 1L; @Inject MongoApplicationScope mongoAccess; public void mongoDzialanie() { DBCollection coll = mongoAccess.getMu().getDb().getCollection("oko"); //at this step everything is correct System.out.println(coll.getCount()); //new connection is created text from mongoDB console -&gt; connection accepted from 127.0.0.1:57700 #2 (2 connections now open) } </code></pre> <p>Why even if I have the same "db" object instance I can't get data without creating new connection, why I can't share this connection as it should be due to pooling? }</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload