Every user is a new thread JSF - JBOSS and creates new connection in MongoDb -


i'm trying work mongodb. created in jsf application scoped bean (with cdi).

@named("appmongo") @applicationscoped public class mongoapplicationscope implements serializable{  private static final long serialversionuid = 1l; 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 db getdb() { return db; }  public dbcollection getcollectionindatabase(string collection) {     dbcollection coll;     coll = db.getcollection(collection);     return coll; }  public mongoclient getmongoclient() {     return mongoclient; } } 

then create request scoped bean using prevoius bean.

@named("mongobean") @requestscoped public class mongobean implements serializable {  private static final long serialversionuid = 1l; @inject mongoapplicationscope mongoaccess; public void pringnumber() { system.out.println(mongoaccess.getcollectionindatabase("mydb").getcount());  } } 

on xhtml page have commandbutton actionlistener calls "printnumber" method. in result each user connect jboss server , click on button open new connection mongo. don't want kind situation. achive situation have 1 connection bettwen server , db, , each user reuse connection.

the mongodb driver maintains own connection pool. connections can't , shouldn't shared between request (think of server side cursors shared, too). however, connections reused after handed pool. until used again, kept alive period of time, since new connection database isn't cheap acquire. tcp 3 way handshake takes quite time, keeping connections open make sense. in fact, in rdbms have walk mile achieve desired behaviour, example c3p0 or dbcp.

for reference: connection pool size (and number of potentially open connections database server) should @ least number of executor threads of servlet engine nobody has wait other request finish before database connection available (not mention queried data). full blown application server, i'd add 20-50 connections, depending on configuration. way go, wether using mongodb or not. depending on deployment, these 200-550 connections.

the side:

  1. mongodb connections pretty lightweight in terms of ram. on client side, talking of few kb per idle connection, on server side 1mb allocated.
  2. you can configure driver on how many connections use. warned fiddling number of connections may have severe consequences in terms of performance under load. please see the api docs of mongoclientoptions.builder details.
  3. the default values of driver rather conservative in terms of resource usage. example, maximum number of connections per host defaults set 100, serious application bit low.

to make long story short: unless experiencing problems, never touch running system.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -