GWT Jetty Jboss J2EE DataSource
Par PlaceOweb le jeudi, avril 23 2009, 13:14 - JAVA - Lien permanent
Comment utiliser une datasource sql aussi bien sous Jetty lors de vos test avec le Devel Mode de GWT que sous Jboss une fois déployé en production ?
Jetty
Jetty datasource en devel mode
Voyez le pré-requis pour que Jetty puisse utiliser la JNDI avec une configuration supplémentaire au démarrage de Jetty.
La configuration nécessaire pour activer vos datasources
/war/WEB-INF/jetty-env.xml pour spécifier une DataSource
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" " http://jetty.mortbay.org/configure.dtd"> <Configure class="org.mortbay.jetty.webapp.WebAppContext"> <New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource"> <Arg>jdbc/projectDS</Arg> <Arg> <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"> <Set name="Url">jdbc:mysql://localhost:3306/database </Set> <Set name="User">myUserName</Set> <Set name="Password">myPassword</Set> </New> </Arg> </New> </Configure>
/war/WEB-INF/web.xml
<resource-ref> <description>DataSource's project</description> <res-ref-name>jdbc/projectDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
Jboss
La configuration nécessaire pour activer vos datasources
jboss-4.2.3.GA/server/default/deploy/project-ds.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ --> <!-- Datasource config for MySQL using 3.0.9 available from: http://www.mysql.com/downloads/api-jdbc-stable.html --> <datasources> <local-tx-datasource> <jndi-name>projectDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/database</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>myUserName</user-name> <password>myPassword</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <!-- should only be used on drivers after 3.22.1 with "ping" support <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name> --> <!-- sql to call when connection is created <new-connection-sql>some arbitrary sql</new-connection-sql> --> <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql> --> <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml --> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
/war/WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"> <jboss-web> <resource-ref> <res-ref-name>jdbc/projectDS</res-ref-name> <jndi-name>java:/projectDS</jndi-name> </resource-ref> <!-- http://docs.jboss.org/jbossas/jboss4guide/r4/html/ch9.chapt.html --> </jboss-web>
Partie commune
/war/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app> <!-- JDBC DataSources (java:comp/env/jdbc) --> <resource-ref> <description>The default DS</description> <res-ref-name>jdbc/projectDS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
Autres ressources
NamingException - Name comp/env/jdbc not Found in Context "java:"
NamingException - Name comp/env/jdbc not Found in Context "java:"
The first type of JNDI lookup is a direct, or global, JNDI lookup.
DataSource ds = (DataSource)ctx.lookup("jdbc/datasourcename");
The second type of JNDI lookup is an indirect, or local, JNDI lookup. The indirect JNDI lookup would look like this:
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/datasourcename");