● tomcat server.xml
--------------------------------------------------------------------
<Context path="/neouser" docBase="neouser"
debug="5" reloadable="false" crossContext="true">
<Resource name="jdbc/SampleDS" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/SampleDS">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>5</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>sample</value>
</parameter>
<parameter>
<name>password</name>
<value>samplepwd</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/sample</value>
● ConnectionManager.java
--------------------------------------------------------------------
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ConnectionManager {
protected static final Log logger = LogFactory.getLog(ConnectionManager.class);
public static Connection getConnection() {
try {
Context ctx = new InitialContext();
// Tomcat Server에서 DataSource를 Lookup
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sampleDS");
return ds.getConnection();
} catch (NamingException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
throw new DBNotConnectedException(e.getMessage(), e);
} catch (SQLException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
throw new DBNotConnectedException(e.getMessage(), e);
}
}
}
이런류의 방법도 이제 고전이 되어버렸지만 그래도 유사방법을 만들어 내기기에는
가장 좋은것 같다.