2011-05-25 93 views
1

我正在使用MySQL在我的GWT应用程序中建立数据库连接。我希望数据库读取一组'init'参数,以便我不需要'手动编写'DB URL和用户名和密码。 我搜索了一段时间寻找可能的解决方案,但却被可能的解决方案所困惑。他们中的一些人谈到了JNDI的解决方案,但没有明确说明如何去做。GWT中用于数据库连接的初始化参数

此外,在Eclipse中以开发/调试模式(在Jetty中)运行应用程序并将其部署到Tomcat中的差异进一步使我感到困惑。

是否可以在web.xml中指定一组初始化参数?我如何阅读他们?

如果要使用JNDI?我可以逐步简明地总结如何实现此任务?

回答

1

你所寻找的是Web应用程序上下文设置 -

要添加Web应用程序上下文中的数据库信息

要配置码头,您将不得不使用jetty-web.xml配置 -

要做到在您的应用程序的连接,在服务器端使用下列 -

 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}

不知道码头-web.xml将只有{}资源名称工作太

编辑 - 样的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> 

你可以阅读每节的含义。

+0

我在执行此操作时遇到错误503“服务不可用”错误。可能的原因是什么? – 2011-05-26 05:27:11

+0

我正在关注此资源,因为它是 - > http://humblecode.blogspot.com/2009/05/gwt-16-using-jndi-datasource.html – 2011-05-26 05:47:01

+0

谢谢!我解决了这个问题。:) – 2011-05-26 06:51:02