I am working on establishing Database connection in my GWT Application with MySQL. I want the database to read a set of 'init' parameters so as I dont need to 'hand code' DB URL and Usernames and Passwords. I googled a while for possible solutions but got a bit overwhelmed by the possible solutions. Some of them talked of JNDI as solution but none where clear as to how to do it.
Moreover the differences in running your application from Eclipse in development/debugging mode(in Jetty) and finally deploying it in Tomcat is further confusing me.
Is it possible to specify set of Init Parameters in web.xml ?开发者_JAVA百科 How do I read them?
If JNDI is to be used? Can I get a step by step concise summary of how to achieve this task?
What you are looking for is web app context setting -
To add the database info in web app context
- http://wiki.metawerx.net/wiki/Context.xml
- this file goes in your war/META-INF directory
To configure jetty, you will have to use the jetty-web.xml configuration also -
- http://docs.codehaus.org/display/JETTY/jetty-web.xml
- This goes in your war/WEB-INF directory
To do a connection in your app, on the server side use the following -
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/{Res Name in context.xml}");
Connection conn = ds.getConnection();
NOTE - To have both your app run both with jetty and tomcat,have both the files and make sure your resource name is-
context.xml : {resourceName}
jetty-web.xml: java:comp/env/{resourceName}
Not sure if jetty-web.xml will work with just {resourcename} too
EDIT - sample context.xml code -
<Resource name="jdbc/myDatabaseServer" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="USER"
password="PWD" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://myUrl:3306/myDB?autoReconnect=true" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true" autoReconnect="true" validationQuery="select 1" testOnBorrow="true"
testOnReturn="true" testWhileIdle="true" timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="3" minEvictableIdleTimeMillis="1800000"/>
Same sample jetty-web.xml code -
<New id="someid" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>java:comp/env/jdbc/myDatabaseServer</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://myUrl:3306/myDB?autoReconnect=true</Set>
<Set name="username">USER</Set>
<Set name="password">PWD</Set>
<Set name="maxActive">100</Set>
<Set name="maxIdle">30</Set>
<Set name="minIdle">0</Set>
<Set name="maxWait">10000</Set>
<Set name="minEvictableIdleTimeMillis">1800000</Set>
<Set name="timeBetweenEvictionRunsMillis">1800000</Set>
<Set name="numTestsPerEvictionRun">3</Set>
<Set name="testOnBorrow">true</Set>
<Set name="testWhileIdle">true</Set>
<Set name="testOnReturn">true</Set>
<Set name="validationQuery">SELECT 1</Set>
</New>
</Arg>
</New>
You can read up on what each sections mean.
精彩评论