如何解决GWT中用于数据库连接的初始化参数
| 我正在使用MySQL在我的GWT应用程序中建立数据库连接。我希望数据库读取一组\'init \'参数,以便不需要\'hand code \'数据库URL,用户名和密码。 我在谷歌上搜索了可能的解决方案,但对可能的解决方案有点不知所措。他们中的一些人谈论JNDI作为解决方案,但不清楚如何做到这一点。 此外,在开发/调试模式下(在Jetty中)从Eclipse运行您的应用程序以及最终在Tomcat中部署它的差异进一步使我感到困惑。 是否可以在web.xml中指定一组初始化参数?我该如何阅读? 如果要使用JNDI?我可以逐步了解如何完成此任务的简要摘要吗?解决方法
您正在寻找的是Web应用程序上下文设置-
在Web应用程序上下文中添加数据库信息
http://wiki.metawerx.net/wiki/Context.xml
该文件位于您的war / META-INF目录中
要配置jetty,您还必须使用jetty-web.xml配置-
http://docs.codehaus.org/display/JETTY/jetty-web.xml
这进入您的war / WEB-INF目录
要在您的应用中建立连接,请在服务器端使用以下命令-
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(\"java:comp/env/{Res Name in context.xml}\");
Connection conn = ds.getConnection();
注意 -
要使您的应用程序同时与jetty和tomcat一起运行,请同时拥有两个文件并确保您的资源名称为-
context.xml : {resourceName}
jetty-web.xml: java:comp/env/{resourceName}
不确定jetty-web.xml是否也可以仅与{resourcename}一起使用
编辑-示例context.xml代码-
<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\"/>
相同的示例jetty-web.xml代码-
<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>
您可以阅读每个部分的含义。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。