Skip to content

WebService JNDI Setting tomcat

neois edited this page Feb 4, 2020 · 2 revisions

Tomcat → JEUS https://waspro.tistory.com/347 Tomcat의 경우, $CATALINA_HOME/conf/server.xml의 <GlobalNameingResources>, <ResourceLink> 설정과 더불어, web application의 web.xml에 설정된 resource reference 설정을 이용한다.

  1. Server.xml

    <GlobalNamingResources>
        <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
          <Resource auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                    maxActive="20" maxIdle="10" maxWait="-1" name="datasource_name" username="user"  password="pass" type="javax.sql.DataSource"
                    url="jdbc:sqlserver://xxxx:portnumber;DatabaseName=dbname" defaultAutoCommit="false"/>
    </GlobalNamingResources>
    <Context docBase="lotteWebService" path="/" reloadable="true" source="org.eclipse.jst.jee.server:lotteWebService">
         <Resource auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
         maxActive="20" maxIdle="10" maxWait="-1" name="datasource_gms" username="user"  password="pass" type="javax.sql.DataSource"
          url="jdbc:sqlserver://ip:port;DatabaseName=dbname" defaultAutoCommit="false"/>
     </Context>
  2. tomcat context.xml copy to resource/META-INT/ and add below <ResourceLink global="datasource_name" name="datasourcename" type="javax.sql.DataSource"/>

  3. sample page Connection conn = null; ResultSet rs = null; PreparedStatement pstmt = null; String str =""; Context ctx = new InitialContext(); Context envCtx = (Context)ctx.lookup("java:comp/env"); DataSource ds = (DataSource)envCtx.lookup("datasource_gms"); conn = ds.getConnection(); pstmt = conn.prepareStatement("select getdate()"); rs = pstmt.executeQuery(); while(rs.next()){ str = rs.getString(1); } System.out.println(str);

 — JEUS 인 경우 web.xml에 아래 추가 <resource-ref> <description>Datasource Contents Server</description> <res-ref-name>JNDI_name</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

Clone this wiki locally