3 Aralık 2008 Çarşamba

Java web uygulamasında web server data source kullanımı

Web uygulamasında web server data source kullanımı için:

1.web serverda data source oluşturuluyor.(web logic için console arayüzü kullanılabilir.)

2.Uygulama web.xml de resource-ref elmanı tanımlanıyor. Bu resource-ref elmanı dış bir kaynağa(burada data source) web uygulamasıreferansı tanımı için kullanılıyor. Buradaki res-ref-name elmanı(XXX) web server da oluşturulan data source jndi name i olacak.
Ör:
<resource-ref>
<res-ref-name>XXX</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3.Connection ın alındığı ilgili sınıfta, connection ı döndüren metodta javax.naming.Context objesi oluşturup bu objeninlookup metoduna web server da oluşturulan data source jndi name'ini parametre olarak geçerek javax.sql.DataSource objesi elde ediliyor.
Ör:
public static synchronized Connection getConnection() throws Exception {
try
{
Context init = new InitialContext();
Context ctx = (Context) init.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("XXX");
Connection connection = ds.getConnection();
return (connection);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
}