230325 수정
요청 - 드라이버로드 - 커넥션 객체생성 - 연결 - 종료
과정을 거치며 서버 자원 낭비 발생 public static void main(String[] args) {
String ip = "ip";
int port = 포트;
String database = "DB명";
String user = "USER명";
String password = "비밀번호";
String DRIVER_NAME = "DB DRIVER_NAME";
String JDBC_URL = "jdbc:해당 DB:thin:@" + ip + ":" + port + ":" + database;
String sql = "SELECT * FROM TEST";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(JDBC_URL, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString("컬럼이름");
}
} ... (이하 생략) ...
미리
생성해놓고 필요할 때마다 사용하고 반환하는 형식으로 자원 낭비를 줄임<!-- datasource 설정(propertyConfigurer 활용) -->
<alias name="dataSource-${Globals.DbType}" alias="dataSource" />
<!-- alias name에 oracle-dbcp, oracle-jndi, mysql-dbcp, mysql-jndi 기입 -->
<!-- Oracle DBCP -->
<bean id="dataSource-oracle-dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${Globals.DriverClassName}"/>
<property name="url" value="${Globals.Url}" />
<property name="username" value="${Globals.UserName}"/>
<property name="password" value="${Globals.Password}"/>
<property name="maxActive" value="150"/>
<property name="maxWait" value="15000"/>
<property name="validationQuery" value="select 1 from dual"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<!-- 밀리세컨드 단위 interval -->
<property name="timeBetweenEvictionRunsMillis" value="300000"/>
</bean>
<!-- Oracle JNDI -->
<bean id="dataSource-oracle-jndi" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:"/>
</bean>
<bean id="dataSource-mysql"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${Globals.DriverClassName}"/>
<property name="url" value="${Globals.Url}" />
<property name="username" value="${Globals.UserName}"/>
<property name="password" value="${Globals.Password}"/>
<property name="maxActive" value="${Globals.maxActive}"/>
<property name="maxIdle" value="${Globals.maxIdle}"/>
<property name="maxWait" value="${Globals.maxWait}"/>
</bean>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.68</version>
</dependency>
<Resource name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mariadb://localhost:3306/test"
username="root"
password="password"
initialSize="20"
maxTotal="100"
maxIdle="100"
minIdle="20"
maxWaitMillis="30000"
/>
다시 짚고 넘어가는 was
1.Web Application Server
2.동적인 컨텐츠를 제공하기 위해 만들어진 Application Server