|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.mongodb.Mongo
public class Mongo
A database connection with internal pooling. For most application, you should have 1 Mongo instance for the entire JVM. The following are equivalent, and all connect to the local database running on the default port:
Mongo instances have connection pooling built in - see the requestStart and requestDone methods for more information. http://www.mongodb.org/display/DOCS/Java+Driver+ConcurrencyMongo mongo1 = new Mongo( "127.0.0.1" ); Mongo mongo2 = new Mongo( "127.0.0.1", 27017 ); Mongo mongo3 = new Mongo( new DBAddress( "127.0.0.1", 27017, "test" ) ); Mongo mongo4 = new Mongo( new ServerAddress( "127.0.0.1") );
You can connect to a replica pair using the Java driver by passing two DBAddresses to the Mongo constructor. For example:
DBAddress left = new DBAddress("localhost:27017/test"); DBAddress right = new DBAddress("localhost:27018/test"); Mongo mongo = new Mongo(left, right);
If the master of a replica pair goes down, there will be a brief lag before the slave becomes master. Thus, your application should be prepared to catch the exceptions that might be thrown in such a case: IllegalArgumentException, MongoException, and MongoException.Network (depending on when the connection drops).
Once the slave becomes master, the driver will begin using that connection as the master connection and the exceptions will stop being thrown.
You can connect to a replica set using the Java driver by passing several a list if ServerAddress to the Mongo constructor. For example:
Listaddrs = new ArrayList (); addrs.add( new ServerAddress( "localhost" , 27017 ) ); addrs.add( new ServerAddress( "localhost" , 27018 ) ); addrs.add( new ServerAddress( "localhost" , 27019 ) ); Mongo mongo = new Mongo( addrs );
By default, all read and write operations will be made on the master. But it's possible to read from the slave(s) by using slaveOk:
mongo.slaveOk();
Nested Class Summary | |
---|---|
static class |
Mongo.Holder
Mongo.Holder is if you want to have a static place to hold instances of Mongo security is not enforced at this level, so need to do on your side |
Field Summary | |
---|---|
static int |
MAJOR_VERSION
|
static int |
MINOR_VERSION
|
Constructor Summary | |
---|---|
Mongo()
|
|
Mongo(List<ServerAddress> replicaSetSeeds)
Creates a Mongo connection. |
|
Mongo(List<ServerAddress> replicaSetSeeds,
MongoOptions options)
Creates a Mongo connection. |
|
Mongo(MongoURI uri)
Creates a Mongo connection. |
|
Mongo(ServerAddress addr)
Connects to a (single) mongodb node |
|
Mongo(ServerAddress addr,
MongoOptions options)
Connects to a (single) mongo node using a given ServerAddress |
|
Mongo(ServerAddress left,
ServerAddress right)
Creates a Mongo connection in paired mode. |
|
Mongo(ServerAddress left,
ServerAddress right,
MongoOptions options)
Creates a Mongo connection in paired mode. |
|
Mongo(String host)
Connects to a (single) mongodb node (default port) |
|
Mongo(String host,
int port)
Connects to a (single) mongodb node |
|
Mongo(String host,
MongoOptions options)
Connects to a (single) mongodb node (default port) |
Method Summary | |
---|---|
void |
addOption(int option)
|
void |
close()
closes all open connections this Mongo cannot be re-used |
static DB |
connect(DBAddress addr)
|
String |
debugString()
|
void |
dropDatabase(String dbName)
Drops the database if it exists. |
ServerAddress |
getAddress()
Gets the address of this database. |
List<ServerAddress> |
getAllAddress()
|
String |
getConnectPoint()
|
List<String> |
getDatabaseNames()
|
DB |
getDB(String dbname)
|
int |
getOptions()
|
String |
getVersion()
|
WriteConcern |
getWriteConcern()
Get the write concern for this database. |
void |
resetOptions()
|
void |
setOptions(int options)
|
void |
setWriteConcern(WriteConcern concern)
Set the write concern for this database. |
void |
slaveOk()
makes this query ok to run on a slave node |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int MAJOR_VERSION
public static final int MINOR_VERSION
Constructor Detail |
---|
public Mongo() throws UnknownHostException, MongoException
UnknownHostException
MongoException
public Mongo(String host) throws UnknownHostException, MongoException
host
- server to connect to
UnknownHostException
- if the database host cannot be resolved
MongoException
public Mongo(String host, MongoOptions options) throws UnknownHostException, MongoException
host
- server to connect tooptions
- options to use
UnknownHostException
- if the database host cannot be resolved
MongoException
public Mongo(String host, int port) throws UnknownHostException, MongoException
host
- the database's host addressport
- the port on which the database is running
UnknownHostException
- if the database host cannot be resolved
MongoException
public Mongo(ServerAddress addr) throws MongoException
addr
- the database address
MongoException
ServerAddress
public Mongo(ServerAddress addr, MongoOptions options) throws MongoException
addr
- the database address
MongoException
ServerAddress
public Mongo(ServerAddress left, ServerAddress right) throws MongoException
Creates a Mongo connection in paired mode.
This will also work for
a replica set and will find all members (the master will be used by
default).
left
- left side of the pairright
- right side of the pair
MongoException
ServerAddress
public Mongo(ServerAddress left, ServerAddress right, MongoOptions options) throws MongoException
Creates a Mongo connection in paired mode.
This will also work for
a replica set and will find all members (the master will be used by
default).
left
- left side of the pairright
- right side of the pair
MongoException
ServerAddress
public Mongo(List<ServerAddress> replicaSetSeeds) throws MongoException
Creates a Mongo connection.
This will work for
a replica set, or pair, and will find all members (the master will be used by
default).
replicaSetSeeds
- Put as many servers as you can in the list and
the system will figure out the rest.
MongoException
ServerAddress
public Mongo(List<ServerAddress> replicaSetSeeds, MongoOptions options) throws MongoException
Creates a Mongo connection.
This will work for
a replica set, or pair, and will find all members (the master will be used by
default).
replicaSetSeeds
- put as many servers as you can in the list.
the system will figure the rest out
MongoException
ServerAddress
public Mongo(MongoURI uri) throws MongoException, UnknownHostException
MongoException
UnknownHostException
examples:
- mongodb://localhost
- mongodb://fred:foobar@localhost/
Method Detail |
---|
public static DB connect(DBAddress addr)
public DB getDB(String dbname)
public List<String> getDatabaseNames() throws MongoException
MongoException
public void dropDatabase(String dbName) throws MongoException
dbName
- name of database to drop
MongoException
public String getVersion()
public String debugString()
public String getConnectPoint()
public ServerAddress getAddress()
public List<ServerAddress> getAllAddress()
public void close()
public void setWriteConcern(WriteConcern concern)
WriteConcern
for more information.
concern
- write concern to usepublic WriteConcern getWriteConcern()
public void slaveOk()
public void addOption(int option)
public void setOptions(int options)
public void resetOptions()
public int getOptions()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |