스프링 기본 테스트
pom.xml
MongoDB 커넥션 얻기org.mongodb mongo-java-driver 2.7.1
public class MongoDbConnection { public DB getMongoConnection() throws UnknownHostException, MongoException { // ReplicaSet 이 아닌 경우는 아래와 같이 간단하게 DB 커넥션을 가져올 수 있다. Mongo mongo = new Mongo("10.13.229.32"); DB db = mongo.getDB("database"); return db; } }JUnit TEST
public class MongoTest { @Test public void insert() throws UnknownHostException, MongoException { MongoDbConnection mongoConnection = new MongoDbConnection(); DB mongo = mongoConnection.getMongoConnection(); DBCollection coll = mongo.getCollection("toolObj"); BasicDBObject doc = new BasicDBObject("name", "Oven"). append("tUrl", "http://oven.ftdev.daum.net"). append("iUrl", "http://oven.ftdev.daum.net"). append("desc", "웹 프로토타이핑 & 커뮤니케이션 툴"). append("team", new BasicDBObject("id", 1111).append("name", "웹표준기술팀")). append("assistor", new BasicDBObject("id", "D0031").append("name", "조규태")). append("use", 1); coll.insert(doc); } @Test public void get() throws UnknownHostException, MongoException { MongoDbConnection mongoConnection = new MongoDbConnection(); DB mongo = mongoConnection.getMongoConnection(); DBCollection coll = mongo.getCollection("toolObj"); BasicDBObject tool = new BasicDBObject("name","Oven"); DBObject oneTool = coll.findOne(tool); System.out.println(oneTool); }
/* 결과 { "_id" : { "$oid" : "51ee3a733004d6c654bdf2c5"} , "name" : "Oven" , "url" : "http://oven.ftdev.daum.net" , "desc" : "웹 프로토타이핑 & 커뮤니케이션 툴" , "team" : { "id" : 1111 , "name" : "웹표준기술팀"} , "assistor" : { "id" : "D0031" , "name" : "조규태"} , "use" : 1 }
@Test public void getCollectionNames() throws UnknownHostException, MongoException { MongoDbConnection mongoConnection = new MongoDbConnection(); DB mongo = mongoConnection.getMongoConnection(); SetMonjaDB (이클립스Plugin, MongoDB UI 툴) 데이터 확인 추가적인 디비 사용법 오브젝트 정의colls = mongo.getCollectionNames(); for (String s : colls) { System.out.println(s); } } }
BasicDBObject info = new BasicDBObject(); info.put("name", "monogdb"); info.put("type", "database");Java Driver Concurrency The Java MongoDB driver is thread safe. If you are using in a web serving environment, for example, you should create a single MongoClient instance, and you can use it in every request. The MongoClient object maintains an internal pool of connections to the database (default pool size of 10). For every request to the DB (find, insert, etc) the Java thread will obtain a connection from the pool, execute the operation, and release the connection. This means the connection (socket) used may be different each time. Additionally in the case of a replica set with slaveOk option turned on, the read operations will be distributed evenly across all slaves. This means that within the same thread, a write followed by a read may be sent to different servers (master then slave). In turn the read operation may not see the data just written since replication is asynchronous. If you want to ensure complete consistency in a “session” (maybe an http request), you would want the driver to use the same socket, which you can achieve by using a “consistent request”. Call requestStart() before your operations and requestDone() to release the connection back to the pool:
DB db...; db.requestStart(); try { db.requestEnsureConnection(); code.... } finally { db.requestDone(); }DB and DBCollection are completely thread safe. In fact, they are cached so you get the same instance no matter what. WriteConcern Option for Single Write Operation Since by default a connection is given back to the pool after each request, you may wonder how calling getLastError() works after a write. You should actually use a write concern like WriteConcern.SAFE instead of calling getLastError() manually. The driver will then call getLastError() before putting the connection back in the pool.
DBCollection coll...; coll.insert(..., WriteConcern.SAFE); // is equivalent to DB db...; DBCollection coll...; db.requestStart(); try { coll.insert(...); DBObject err = db.getLastError(); } finally { db.requestDone(); }
'History' 카테고리의 다른 글
[MongoDB] Spring JSON 응답처리 설정 방법 정리 - TEXT (0) | 2014.06.05 |
---|---|
[MongoDB] Spring JSON response, JSON view - TEXT (0) | 2014.06.05 |
[MongoDB] Spring JSON 응답처리 설정 방법 정리 (0) | 2014.06.05 |
[MongoDB] Spring JSON response, JSON view (0) | 2014.06.05 |
java-mongo-driver CRUD 예제 (0) | 2014.06.05 |