Configurare JDBC DataSource in TomCat

mongooseriverΛογισμικό & κατασκευή λογ/κού

7 Ιουν 2012 (πριν από 5 χρόνια και 4 μήνες)

488 εμφανίσεις

Progettazione Applicazioni Web


Progettazione Applicazioni Web

Guide



Configurare JDBC Data
S
ource in
TomC
at

Questa guida è un estratto del tutorial presente in
http://tomcat.apache.org/tomcat
-
5.5
-
doc/jndi
-
resources
-
howto.html#JDBC%20Data%20Sources

.

Installazione del

JDBC Connector


Tomcat ha bisogno di una lib
reria, denominata connector,
per

accedere
a

un database MySQL.

1.

Scaricare Connector/J da
http://dev.mysql.com/downloads/connector/j/5.1.html

2.

Scompattare

il file zip (o tar.gz) scarica
to
.

3.

Copiare

il file
mysql
-
connector
-
java
-
5.1.10
-
bin.jar

che si trova nella sottocartella src nel punto in
cui abbiamo scompattato il file.

4.

Incollare

il file nella sottocartella
lib

di tomcat.

a.


Per cercare dove è installato tomcat:

i.

Aprire netbeans
;

ii.

Seleziona
re il menù

Tools
-
>Servers
;

iii.

Apparirà la finestra di configurazione dei server
;

iv.

Il path cercato è la proprietà
Catalina Home
.


5.

Riavviare

Tomcat / Netbeans per attivare le modifiche.

Modifica web.xml

1.

Dato un progetto in netbeans, aprire il file
web.xml

che si

trova nella cartella
WEB
-
INF


2.

Selezionare
la

modalità di editing xml

3.

Defi
nire una risorsa per accedere a

un oggetto
DataSource

a.

Incollare il se
g
uente codice tra i tag <web
-
app></web
-
app>

<resource
-
ref>

<description>DB Connection</description>

<
res
-
ref
-
name>
jdbc/TestDB
</res
-
ref
-
name>

<res
-
type>
javax.sql.DataSource
</res
-
type>

<res
-
auth>Container</res
-
auth>

</resource
-
ref>

Modifica context.xml

1.

Aprire il file
context.xml
che si trova nella cartella

META
-
INF

del progetto.

2.

Selezionare
la
modalità di editing xml

3.

Immettere i parametri di configurazione dell’oggetto Data Source
:

a.

Incollare il seguente codice tra i tag <Context></Context>

<Resource name="
jdbc/TestDB
" auth="Container"
type="javax.sql.DataSource”

maxActive="100" maxIdle="30" maxWa
it="10000"
username="
utente1
" password="
utente1
"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://
localhost
:3306/
nazioni
?autoReconnect=true"/>


Progettazione Applicazioni Web


Progettazione Applicazioni Web

Guide



Accedere al pool di connessioni al Database

Una volta configurato corret
tamente Tomcat modificando i file web.xml e context.xml è possibile per
qualsiasi oggetto java o pagina
JSP

ottenere una connessione al database

prendendola

in prestito


dal
pool

di connessioni che è gestito direttamente da Tomcat
.

Context initContext = new InitialContext();

Context envContext =
(Context)initContext.lookup("java:/comp/env
");

DataSource ds = (DataSource)envContext.lookup("
jdbc/TestDB
");

Connection conn = ds.getConnection();

Appendice
:

estratto di
J
avaDoc

public interface DataSource

A factory for connections to the physical data source that this
DataSource

object represents. An alternative to the
DriverManager

facility, a
DataSource

object is the preferred means of getting a connection. An object that
implements the
DataSource

interface will typically be registered with a naming service based on the Java
TM

Naming
and Directory (JNDI) API.

The
DataSource

interface is implemented by a driver vendor. There are three types of implementations:

1.

Basic implementation
--

produces a standard
Connection

object

2.

Connection pooling implementation
--

produces a
Connecti
on

object that will automatically participate in
connection pooling. This implementation works with a middle
-
tier connection pooling manager.

3.

Distributed transaction implementation
--

produces a
Connection

object that may be used for distributed
transacti
ons and almost always participates in connection pooling. This implementation works with a middle
-
tier transaction manager and almost always with a connection pooling manager.

A
DataSource

object has properties that can be modified when necessary. For exa
mple, if the data source is moved
to a different server, the property for the server can be changed. The benefit is that because the data source's properties
can be changed, any code accessing that data source does not need to be changed.

A driver that is

accessed via a
DataSource

object does not register itself with the
DriverManager
. Rather, a
DataSource

object is retrieved though a lookup operation and then used to create a
Connection

object. With a
basic implementation, the connection obtained through
a
DataSource

object is identical to a connection obtained
through the
DriverManager

facility.


public interface Context

This interface represents a naming context, which consists of a set of name
-
to
-
object bindings.
It contains methods for
examining and u
pdating these bindings.

Names

Each name passed as an argument to a
Context

method is relative to that context. The empty name is
used to name the context itself. A name parameter may never be null.

Most of the methods have overloaded versions with one ta
king a
Name

parameter and one taking a
String
. These
overloaded versions are equivalent in that if the
Name

and
String

parameters are just different representations of the
same name, then the overloaded versions of the same methods behave the same. In the
method descriptions below, only
one version is fully documented. The second version instead has a link to the first: the same documentation applies to
both.

For systems that support federation,
String

name arguments to
Context

methods are composite names.

Name
arguments that are instances of
CompositeName

are treated as composite names, while
Name

arguments that are not
instances of
CompositeName

are treated as compound names (which might be instances of
CompoundName

or
other implementations of compound names). This allows the results of
NameParser.parse()

to be used as
arguments to the
Context

methods. Prior to JNDI 1.2, all name arguments were treated as composite names.

Furthermore, for systems that support federa
tion, all names returned in a
NamingEnumeration

from
list()

and
listBindings()

are composite names represented as strings.
See
CompositeName

for the string syntax of
names.

Progettazione Applicazioni Web


Progettazione Applicazioni Web

Guide



For systems that do not support federation, the name arguments (in either
Name

or
String

forms) and the names
returned in
NamingEnumeration

may be names in their own namespace rather than names in a composite
namespace, at the discretion of the service provider.