在我的RESTful WS开发中,这是非常奇怪的。我使用Tomcat 7和Jersey1.8,Spring2.5和MySQL数据库。
我定义了两个像这样的数据源:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="jdbc/MAINDB" />
<property name="resourceRef" value="true" />
</bean>
<bean id="orderDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<property name="jndiName" value="jdbc/ORDERS" />
<property name="resourceRef" value="true" />
</bean>并将一个数据源注入servlet上下文,如下所示:
<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
<property name="attributes">
<map>
<entry key="orderData">
<ref bean="orderDataSource" />
</entry>
</map>
</property>
</bean>这个代码是:
orderDataSource = (DataSource) sc.getServletContext().getAttribute("orderData");
Order ord = new Order();
Statement stmt = null;
try {
stmt = orderDataSource.getConnection().createStatement();
} catch (SQLException ex) {
Logger.getLogger(OrdersResource.class.getName()).log(Level.SEVERE, null, ex);
}得到6或7次完美的执行,从第8次请求开始,它会在createStatement()行停止运行,永远不会返回或抛出任何异常!
但是,MAINDB的所有其他资源API仍然工作得很好。
有人知道发生了什么吗?
发布于 2014-05-23 05:58:13
你为什么要为这样的代码烦恼呢?如果您已经在使用Spring,请使用JdbcTemplate。它可以使用您的DataSource初始化,并将为您管理所有这些。
您需要从池中连接,但从未关闭它(实际上,语句也是如此)。首先,使用JdbcTemplate可以防止出现这些问题。
有点像
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute(...)有关更多详细信息,请参阅javadoc。
https://stackoverflow.com/questions/23813833
复制相似问题