๐ JNDI๋? (Java Naming and Directory Interface)
Java EE์์ ์ฌ์ฉ๋๋ ๋ค์ด๋ฐ ์๋น์ค๋ก, ๋ฆฌ์์ค (DataSource, JMS ๋ฑ)๋ฅผ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฝ๊ฒ ์ฐพ๊ณ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ค.
โ 1. ๊ธฐ๋ณธ ๊ฐ๋
- DataSource
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด๋ก, ์ง์ DriverManager.getConnection()์ ์ฌ์ฉํ๋ ๋์ JNDI๋ฅผ ํตํด ์ ๊ณต๋จ.
- Connection pool์ ์ดํ๋ฆฌ์ผ์ด์ ๋จ์์ ์ด๋ป๊ฒ ๊ด๋ฆฌํ ์ง๋ฅผ ๊ตฌํํ๋ ์ธํฐํ์ด์ค
โญDriverManager.getConnection():์ค์ ์๋ฐ ํ๋ก๊ทธ๋จ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋คํธ์ํฌ์์์ ์ฐ๊ฒฐํด์ฃผ๋ ๋ฉ์๋์ด๋ค.
- Connection: ๋คํธ์ํฌ ์์ ์ฐ๊ฒฐ ์์ฒด๋ฅผ ์๋ฏธ
- Connection Pool (์ปค๋ฅ์ ํ): ํด๋ผ์ด์ธํธ์ ์์ฒญ ์์ ์ Connection์ ์ฐ๊ฒฐํ๋ ๊ฒ์ด ์๋๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ๋ฏธ๋ฆฌ ์์ฑํด ๋๊ณ ํ์ํ ๋ ๊ฐ์ ธ๋ค ์ฐ๋ ๋ฐฉ์์ผ๋ก, ์ฑ๋ฅ ํฅ์ ๋ฐ ๋ฆฌ์์ค ์ ์ฝ์ ์ํ ๊ธฐ๋ฒ.
๐ฅJNDI VS DBCP VC JDBC
๊ตฌ๋ถ | JNDI DataSource | DBCP DataSource | JDBC (์ง์ ์ฐ๊ฒฐ) |
์ค์ ๋ฐฉ์ | Tomcat(WAS)์์ JNDI๋ก ์ค์ ๋ DataSource๋ฅผ ์ฐธ์กฐ | ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ง์ **DBCP (Database Connection Pooling)**์ ์์ฑ | ์ ํ๋ฆฌ์ผ์ด์ ์ด JDBC ๋๋ผ์ด๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ DB ์ฐ๊ฒฐ |
Spring ํด๋์ค | org.springframework.jndi.JndiObjectFactoryBean | org.apache.commons.dbcp.BasicDataSource | java.sql.Connection, java.sql.DriverManager |
์ปค๋ฅ์ ๊ด๋ฆฌ | WAS (Tomcat)์์ ์ปค๋ฅ์ ํ ๊ด๋ฆฌ, ์ฌ๋ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ด ๊ฐ์ ์ค์ ๊ณต์ ๊ฐ๋ฅ | ๊ฐ๋ณ ์ ํ๋ฆฌ์ผ์ด์ ๋ด์์ ์ปค๋ฅ์ ํ ์ง์ ์์ฑ ๋ฐ ๊ด๋ฆฌ | ์ปค๋ฅ์ ํ ์์, ๋งค๋ฒ DriverManager.getConnection()์ ํธ์ถํ์ฌ ์ฐ๊ฒฐ ์์ฑ |
โ
2. ์ถ์ฒ ๋ฐฉ์
- JNDI: WAS(Tomcat ๋ฑ)์์ ์ค์์์ ์ปค๋ฅ์ ๊ด๋ฆฌ, ์ด์ ํ๊ฒฝ์์ ์ ์ง๋ณด์ ํธ๋ฆฌ
- DBCP: ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ง์ ์ปค๋ฅ์ ํ ๊ด๋ฆฌ, ์ค์ ์ด ๊ฐํธ
- JDBC: ์ปค๋ฅ์ ํ ์์ด ์ง์ ์ฐ๊ฒฐ, ์ฑ๋ฅ ์ ํ ๊ฐ๋ฅ
โ 3. JNDI ์ค์ ๋ฐฉ๋ฒ (JNDI ์์ )
1๏ธโฃ Context-datasorce.xml
<!-- JNDI ๋ฐฉ์ -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- Oracle JNDI DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/ifn" />
<property name="resourceRef" value="true" />
</bean>
</beans>
<!-- DBCP ๋ฐฉ์ -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<!-- Oracle : ๊ฐ๋ฐ DB -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@11.111.111.11:1521/dbname" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
</bean>
2๏ธโฃ web.xml์์ JNDI ๋ฐ์ดํฐ ์์ค ์ค์ ์ถ๊ฐ
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/ifn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3๏ธโฃ Tomcat Connection Pool ์ค์
๐ Context.xml (๊ถ์ฅ)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- Default set of monitored resources. If one of these changes, the web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- JNDI DataSource ์ค์ ์ถ๊ฐ -->
<Resource name="jdbc/ifn"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@11.111.11.11:1521/dbname"
username="user"
password="password"
maxTotal="10"
maxIdle="5"
maxWaitMillis="10000"/>
</Context>
๐ server.xml (๊ถ์ฅ x)
<GlobalNamingResources>
<Resource name="jdbc/ifn"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:dbname"
username="your_username"
password="your_password"
maxTotal="20"
maxIdle="10"
maxWaitMillis="-1"/>
</GlobalNamingResources>
๐จerror: Caused by: javax.naming.NameNotFoundException: Name [jdbc/ifn]์(๋) ์ด ์ปจํ ์คํธ์ ๋ฐ์ธ๋ฉ๋์ง ์์์ต๋๋ค. [jdbc]์(๋ฅผ) ์ฐพ์ ์ ์์ต๋๋ค.
์์ ์ค์ ์ ๋๋ก ์ ํ์ ๋ ๋ฐ์ํ๋ ์๋ฌ!!
'Backend > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] ๋ฐ์ดํฐ ํ์ Primitive Type vs Wrapped Type (0) | 2025.02.26 |
---|---|
static ํค์๋ (0) | 2025.02.26 |
์๋ฐ(Spring) ๊ธฐ๋ณธ ์์ธ ์ฒ๋ฆฌ (0) | 2025.02.23 |
[Java] ๋น๋๊ธฐ ์์ ์ฒ๋ฆฌ (ExecutorService VS CompletableFuture) (0) | 2025.02.23 |
HttpURLConnection (0) | 2024.06.07 |