Andrea "Kiraya"Magini

IT Professional Master

Single post

Tomcat 7, jndi datasource ed eclipse

Piccola guida per la risoluzione di un banale task ma che spesso crea problemi, e cioè la definizione di un datasource jdbc su Tomcat .
Nel caso specifico utilizzo un datasource con driver SqlServer di Microsoft.
La libreria Microsoft contenente il driver jdbc (nel mio caso la sqljdbc4.jar), deve essere messa nella cartella lib del server tomcat.
Va poi configurata la risorsa jndi nel file server.xml nella cartella conf di tomcat, aggiungendo ad esempio questo :

<!--contenuto vario del nostro file server,trovare l'elemento globalnamingresources e aggiungere la nostra resource-->
<globalNamingResources>
<resource name="jdbc/testdb" auth="Container" type="javax.sql.DataSource"
  username="kiraya"
  password="kiraya"
  factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
  driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url="jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=MYDB"
  maxActive="50"
  maxIdle="10"
  maxWait="15000"
  removeAbandoned="true"
        removeAbandonedTimeout="30"
        logAbandoned="true"
  validationQuery="select 1" />
</globalNamingResources>

Fatto questo, per poter rendere visibile al contesto web della nostra webapplication il driver, senza essere costretti a registrarlo manualmente tramite il DriverManager, mettiamo un resource-link o copiamo direttamente l’xml della definizione resource sopra inserita, nel file context.xml della cartella conf di tomcat, od in un file context.xml nella cartella META-INF della webapplication.
Dovendo rilasciare l’applicazione su diversi application server, preferisco non portare specifici files di configurazione nell’applicazione(ove possibile) e quindi lascio il file context.xml nella cartella config del server tomcat.
Contenuto del context.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<!-- The contents of this file will be loaded for each web application -->
<!-- Default set of monitored resources -->
WEB-INF/web.xml

<!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) -->
<!--
<valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->

Fatto questo, possiamo utilizzare una lookup jndi nella maniera classica per ottenere una connessione db.

javax.naming.InitialContext context = new javax.naming.InitialContext();
DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/testdb");
Connection conn = ds.getConnection();

Se tomcat è configurato come server Runtime dentro eclipse, e quindi lo start e stop viene fatto da li, allora il file context da modificare non è quello della cartella conf del server ma quella della versione gestita da Eclipse, generalmente contenuta nel workspace, nella cartella Server..