안녕세계

[SK고용디딤돌] Node.js (마지막) 본문

[SK고용디딤돌] Node.js (마지막)

Junhong Kim 2016. 8. 19. 18:11
728x90
반응형

conn = mysql.createConnection

conn 은 쓰기/ 읽기용 스트림이다.


db에는 sql parser 라는 것이 존재한다.

sql 문장이 없었으면 optimiser에게 가장 저렴한 실행계획을 만들어 달라고 요청을한다.


optimiser는 여러개의 execution plan을 만든다.

결과를 씀


conn.end()가 db 폭파임..


이것을 개선하기 위해서 object pooling 이라는 전략이 있음

object는 미리 만들어 놓고 빌려서 쓰는 것임. ( 카쉐어링 같은것임)


[ dbPool을 사용할 때 conn.release() 중요 ]

트랜잭션을 관리할떄 commit 할떄 rollback 할때

아니면 에러 일때 마지막 콜백 또는 실제로 db를 데이터 베이스를 다 썻다고 판단했을때 쓰자.


-- 비동기 코드 연습

function getConnection(callback) {
// 사용 가능한 connection 객체를 조회
var index = this.findIndex(function(element) {
return element.usable;
});
// 없을 경우
if (index === -1) {
callback(new Error('사용할 수 있는 데이터베이스 연결 객체가 없습니다.'));
} else {
this[index].usable = false;
callback(null, this[index]);
}
// 있을 경우
}

var dbPool = [];
dbPool.getConnection = getConnection;

for (var i = 0; i < 5; i++) {
dbPool.push({
name: 'connection #' + i,
usable: true
});
}

for (var i = 0; i < 3; i++) {
dbPool[i].usable = false;
}

dbPool.getConnection(function(err, connection) {
if (err) {
return console.log(err);
}
console.log(connection);
});

/*
var dbPool = [];
for (var i = 0; i < 5; i++) {
dbPool.push({
name: 'connection #' + i,
usable: true
});
}

function getConnection(callback) {
// 사용 가능한 connection 객체를 조회
var index = dbPool.findIndex(function(element) {
return element.usable;
});
// 없을 경우
if (index === -1) {
callback(new Error('사용할 수 있는 데이터베이스 연결 객체가 없습니다.'));
} else {
dbPool[index].usable = false;
callback(null, dbPool[index]);
}
// 있을 경우
}

getConnection(function(err, connection) {
if (err) {
return console.log(err);
}
console.log(connection);
});
*/







[로그인이 안되어있을 때





728x90
반응형
Comments