모바일 응용 프로그램에서 요청을 받고 해당 데이터를 사용자에게 표시하는 일부 데이터베이스 작업을 수행하는 J2EE 응용 프로그램을 개발 중입니다.Tomcat 연결 풀링 방법
따라서 데이터베이스 연결 풀링이 나랑에 매우 중요하다는
<Context debug="0"
reloadable="true" crossContext="false" privileged="true" cookies="true" >
<Resource name="jdbc/servicedb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="XXXXXX"
password="XXXXXX"
url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX"
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"
suspectTimeout="60"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"/>
</Context>
을 다음 그리고 이것은 내 연결에서하는 방법입니다 보이는 내 J2EE application.It 내 META-INF 폴더에 context.xml
선언 연결을
public static Connection getConnection()
{
Connection dbConnection = null;
//loading from the dao.properties file
DAOProperties properties = new DAOProperties("com.jndi");
String url = properties.getProperty(PROPERTY_URL, true);
String driverClassName = properties.getProperty(PROPERTY_DRIVER, false);
String password = properties.getProperty(PROPERTY_PASSWORD, false);
String username = properties.getProperty(PROPERTY_USERNAME, password != null);
// If driver is specified, then load it to let it register itself with DriverManager.
if(driverClassName !=null){
try
{
Class.forName(driverClassName);
dbConnection = DriverManager.getConnection(url, username, password);
}catch(ClassNotFoundException e){
// Could not find the database driver
e.printStackTrace();
System.out.println("database driver error");
}catch(Exception e){
e.printStackTrace();
}
}
// Else assume URL as DataSource URL and lookup it in the JNDI.
else{
Context initContext;
DataSource ds;
try {
initContext = new InitialContext();
Context envContext = (Context)initContext.lookup(JNDI_ROOT);
if(envContext!=null){
ds = (DataSource)envContext.lookup(url);
if(ds!=null){
try {
dbConnection = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} catch (NamingException e) {
e.printStackTrace();
}
}
return dbConnection;
}
DAOProperties를 반환 클래스는 속성 믿을수를로드하기위한 래퍼 클래스가 here
에서했다입니다 16,나는이
public static void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
System.err.println("Closing Connection failed: " + e.getMessage());
e.printStackTrace();
}
}
}
같은 연결을 종료하고 마지막으로 난 그냥 지금은 연결 풀링에 this 겪었이
public loadlist(){
Connection dbConnection = null;
try{
dbConnection = Connection.getConnection();
if(dbConnection !=null){
System.out.println("Connected to database");
LOGGER.debug("Successfully connected to database");
else{
System.out.println("No Connection Exists");
LOGGER.debug("unable to connect to database");
}
}catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
System.out.println("database connection failed");
LOGGER.debug("database connection failed due to SQLException");
}finally{
Connection.close(dbConnection);
LOGGER.debug("connection closed");
}
return result;
}
같은 목록을 polulate에 대한 연결을 사용하고 있습니다.
나는 내가 내 연결을 닫거나이 풀에 반환 할 수 있는지 여부를 의심?는 또한 풀에 연결을 반환하는 방법을 알고 싶어?
도와주세요.
: 풀에서가 아닌 풀 된 접속을 처리하는 것과 동일한 방식으로'Connection'을 처리 : 단순히 전화를'close()를'당신이 그것을 사용하여 수행 할 때. –
@ Codo- 응답 해 주셔서 감사합니다. 톰캣에서만 연결 관리 만 담당하게됩니다. 바로 '닫기'방법을 참조하십시오. (http://balusc.blogspot.com/2008/07/dao -tutorial-데이터 layer.html # HowAboutConnectionPooling) – Sreeram
@Sreeram : 귀하의 코멘트에 링크 된 'close()를'방법은 연결 풀을 구현하는 방법을 그림이고 당신은 또는 그것을 사용하는 방법 또는 당신이 당신의 DAO를 구현하는 방법을하지 수업. Tomcat은 연결 생성 시점, 연결 재사용 시점, 풀에 연결을 반환 할 시점 및 닫는 시점을 관리합니다. – Codo