I am working on a GWT Application wh开发者_C百科ich requires connection with MySQL database. I can do it successfully for a servlet. However I require multiple "RemoteServiceServlets" to share a single Conection refrence as creating a new one everytime makes no sense.
How can I achive this?
Sharing a single JDBC connection in a servlet environment where multiple users are accessing it can have serious consequences: http://forums.oracle.com/forums/thread.jspa?threadID=554427
In a nutshell: a single connection represents a single DBMS user doing a single series of queries and/or updates, with one transaction in-force at any given moment.
So basically in a servlet environment you must use a JDBC connection pooling, where you get a new connection from a pool of reusable connections, but a single connection is only used by one servlet at a time. Here is an example implementation: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
If you're willing to use Spring, i would suggest trying the approach described here http://pgt.de/2009/07/17/non-invasive-gwt-and-spring-integration-reloaded/ , which I used for some of my projects in GWT.
Add your spring context configuration to your web.xml file
<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>
Define your datasource in the context file
<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>
Have your servlets extend
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);
}
}
And then use your datasource as a spring bean in all your servlets
public class MyServiceImpl extends AutoinjectingRemoteServiceServlet implements MyService {
  @Autowired
  private DataSource dataSource;
  ...
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论