【发布时间】:2013-12-26 14:42:56
【问题描述】:
我想在 aws 中迁移我的应用程序,
我已经设置了我的环境。使用弹性豆茎。
在我的应用程序的当前版本中,它在本地运行正常,我用这个简单的代码设置连接:
在 web.xml 中我已经定义了资源:
<resource-ref>
<description> Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the <Context> configuration for the web application. </description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后在 config.xml 我定义了资源
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="utente1" password="utente1" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/nazioni?autoReconnect=true" />
现在我可以通过这种方式获得连接
/* JNDI query to locate the DataSource object */
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env") ;
// JNDI standard naming root
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
/* Ask DataSource for a connection */
Connection conn = ds.getConnection();
现在我的问题是:我可以在我的 config.xml 中硬编码连接字符串还是使用 System.getProperty("JDBC_CONNECTION_STRING") 更好?如果是这样,我如何在加载tomcat时设置连接字符串?
谢谢,洛里斯
【问题讨论】:
标签: java mysql tomcat amazon-elastic-beanstalk amazon-rds