2011-05-26 148 views

回答

0

如果你願意使用Spring,我會建議嘗試在這裏描述的方法http://pgt.de/2009/07/17/non-invasive-gwt-and-spring-integration-reloaded/,我在GWT中使用了我的一些項目。

添加您的Spring上下文配置到web.xml文件

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/application-context.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

定義你的數據源的背景下,文件

<bean id="dataSource" destroy-method="close" 
    class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
</bean> 

有你的servlet擴展

public class AutoinjectingRemoteServiceServlet extends RemoteServiceServlet { 

@Override 
public void init(ServletConfig config) throws ServletException { 
    super.init(config); 
    WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()); 
    AutowireCapableBeanFactory beanFactory = ctx.getAutowireCapableBeanFactory(); 
    beanFactory.autowireBean(this); 
} 

} 

然後用您的數據源作爲所有servlet中的spring bean

public class MyServiceImpl extends AutoinjectingRemoteServiceServlet implements MyService { 

    @Autowired 
    private DataSource dataSource; 

    ... 
2

共享在多個用戶訪問它可能會產生嚴重後果的servlet環境中的單個JDBC連接:http://forums.oracle.com/forums/thread.jspa?threadID=554427

一言以蔽之:一個連接代表一個單獨的DBMS用戶做查詢單系列 和/或更新,其中一個交易在任何給定的 時刻生效。

因此,基本上在servlet環境中,您必須使用JDBC連接池,您可以從可重用連接池獲得新連接,但一次連接只能由一個servlet使用。這裏是一個示例實現:http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html

相關問題