출처 : http://hmjkor.tistory.com/408

참고 : https://tomcat.apache.org/tomcat-8.0-doc/jndi-datasource-examples-howto.html

apache tomcat 5.5 이후 버전에서의 Context 생성 방법입니다.


server.xml 파일을 열게 되면 xml Tag가 Service라는 태그의 속성명 name을 찾습니다.

일반적으로는  <Service name="Catalina"> 이렇게 되어 있습니다.


먼저 context를 만들기 위한 폴더를 생성합니다.

service 태그의 name값인 Catalina 폴더를 만들고 다시 그 안에 localhost 라는 폴더를 만듭니다.

최종적으로는 위에 내용대로라면 톰캣경로/conf/Catalina/localhost가 되겠네요


그럼 이제 이하위에 파일을 생성하시면 되는데 파일은 xml 형식입니다.

root Context는 ROOT.xml을 만드시면 되고 기타 context는 컨텍스트명.xml파일로 만드시면 됩니다.


파일이 생성이 되었으면 이제 관련 설정을 파일에 추가하면 됩니다.

test라는 컨텍스트를 만들게 되면 아래와 같은 형식이 되겠습니다.


server.xml

<?xml version='1.0' encoding='utf-8'?>
<Context path="/test"
         docBase="/home/test/web"
         debug="0"
         reloadable="true"
         crossContext="true">

<Valve className="org.apache.catalina.valves.AccessLogValve"
            directory="/home/test/logs/access"
            prefix="access."
            suffix=".log"
            pattern="common"
            fileDateFormat="yyyy-MM-dd"
            resolveHosts="false"/>
    <Resource name="jdbc/my" auth="Container"
                        type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
                        url="jdbc:mysql://[ip]:[port]/[db명]?zeroDateTimeBehavior=convertToNull"
                        username="[username]" password="[password]"
                        loginTimeout="10" maxActive="50"
                        maxIdle="10" maxWait="-1" />
</Context> 


<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@DB_IP:DB_PORT:DB_SID"
    username="아이디" password="비밀번호" maxTotal="20" maxIdle="10" />




Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor.

This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.

The configuration properties for Tomcat's standard data source resource factory (org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory) are as follows:

  • driverClassName - Fully qualified Java class name of the JDBC driver to be used.
  • username - Database username to be passed to our JDBC driver.
  • password - Database password to be passed to our JDBC driver.
  • url - Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
  • initialSize - The initial number of connections that will be created in the pool during pool initialization. Default: 0
  • maxTotal - The maximum number of connections that can be allocated from this pool at the same time. Default: 8
  • minIdle - The minimum number of connections that will sit idle in this pool at the same time. Default: 0
  • maxIdle - The maximum number of connections that can sit idle in this pool at the same time. Default: 8
  • maxWaitMillis - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. Default: -1 (infinite)

Some additional properties handle connection validation:

  • validationQuery - SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row.
  • validationQueryTimeout - Timeout in seconds for the validation query to return. Default: -1 (infinite)
  • testOnBorrow - true or false: whether a connection should be validated using the validation query each time it is borrowed from the pool. Default: true
  • testOnReturn - true or false: whether a connection should be validated using the validation query each time it is returned to the pool. Default: false

The optional evictor thread is responsible for shrinking the pool by removing any connections which are idle for a long time. The evictor does not respect minIdle. Note that you do not need to activate the evictor thread if you only want the pool to shrink according to the configured maxIdle property.

The evictor is disabled by default and can be configured using the following properties:

  • timeBetweenEvictionRunsMillis - The number of milliseconds between consecutive runs of the evictor. Default: -1 (disabled)
  • numTestsPerEvictionRun - The number of connections that will be checked for idleness by the evictor during each run of the evictor. Default: 3
  • minEvictableIdleTimeMillis - The idle time in milliseconds after which a connection can be removed from the pool by the evictor. Default: 30*60*1000 (30 minutes)
  • testWhileIdle - true or false: whether a connection should be validated by the evictor thread using the validation query while sitting idle in the pool. Default: false

Another optional feature is the removal of abandoned connections. A connection is called abandoned if the application does not return it to the pool for a long time. The pool can close such connections automatically and remove them from the pool. This is a workaround for applications leaking connections.

The abandoning feature is disabled by default and can be configured using the following properties:

  • removeAbandoned - true or false: whether to remove abandoned connections from the pool. Default: false
  • removeAbandonedTimeout - The number of seconds after which a borrowed connection is assumed to be abandoned. Default: 300
  • logAbandoned - true or false: whether to log stack traces for application code which abandoned a statement or connection. This adds serious overhead. Default: false

Finally there are various properties that allow further fine tuning of the pool behaviour:

  • defaultAutoCommit - true or false: default auto-commit state of the connections created by this pool. Default: true
  • defaultReadOnly - true or false: default read-only state of the connections created by this pool. Default: false
  • defaultTransactionIsolation - This sets the default transaction isolation level. Can be one of NONEREAD_COMMITTEDREAD_UNCOMMITTEDREPEATABLE_READSERIALIZABLE. Default: no default set
  • poolPreparedStatements - true or false: whether to pool PreparedStatements and CallableStatements. Default: false
  • maxOpenPreparedStatements - The maximum number of open statements that can be allocated from the statement pool at the same time. Default: -1 (unlimited)
  • defaultCatalog - The name of the default catalog. Default: not set
  • connectionInitSqls - A list of SQL statements run once after a Connection is created. Separate multiple statements by semicolons (;). Default: no statement
  • connectionProperties - A list of driver specific properties passed to the driver for creating connections. Each property is given as name=value, multiple properties are separated by semicolons (;). Default: no properties
  • accessToUnderlyingConnectionAllowed - true or false: whether accessing the underlying connections is allowed. Default: false

For more details, please refer to the commons-dbcp documentation.




만약 다른 도메인이나 서브 도메인이 생성되어야 한다면

server.xml에 신규 HOST를 추가한다.

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
    기존 도메인 내용이 있음
</Host>
<!-- 신규로 추가되는 서브 도메인 -->
<Host name="test.anaconda.pe.kr" appBase="webapps" unpackWARs="true" autoDeploy="true">
</Host>



그리고 /conf/Catalina/폴더 아래에 test.anaconda.pe.kr라는 폴더를 만든다.

/conf/Catalina/test.anaconda.pe.kr와 같은 폴더를 가진다.

그리고 그 아래에 ROOT.xml파일을 만들고 context 정보를 넣어주면 된다.



+ Recent posts