global data source

Experimental purpose: In order to study whether two data source objects are created when two projects access a global data source at the same time, or two data source objects are created.

1: Copy the diuid and mysql driver packages (druid-1.0.2.jar and mysql-connector-java-5.1.15.jar) to %TOMCAT_HOME%/lib;
2: Configure the data source and put JNDI in %TOMCAT_HOME% Configured in /conf/context.xml, the format is as follows:
<Resource
        auth="Container"
        driverClassName="com.mysql.jdbc.Driver"
        factory="com.alibaba.druid.pool.DruidDataSourceFactory"
        maxActive="20"
        maxIdel ="10"
        maxWait="1000"
        name="jdbc/snf-paiDS"
        password="adminone"
        type="javax.sql.DataSource"
        url="jdbc:mysql://10.27.82.169/pai?zeroDateTimeBehavior=convertToNull " />
<ResourceLink global="jdbc/snf-paiDS " name="jdbc/snf-paiDS" type="javax.sql.DataSource" />
3: Configure druid's monitoring servletMapping in web.xml:
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name >DruidStatView</servlet-name>
        <url-pattern>/druid/console/*</url-pattern>
    </servlet-mapping>
4: Start the empty tomcat, if the startup is successful, the configuration is correct;
5: In Associate tomcat in eclipse, (note that the configuration after association should be configured according to the above steps).
6: Create an empty project to access the data source.
7: Create a new servlet and configure servletMapping in web.xml, for example:
[JAVA code:]
public class HoldConnection extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private DataSource ds; //declare the data source
    private List<Connection>  c                = new ArrayList<Connection>(); //存放连接,防止被垃圾回收器回收
    public HoldConnection() {
        super();
        try {
            Context ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/snf-paiDS");//访问JNDI
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        try {
            DruidPooledConnection conn = (DruidPooledConnection) ds.getConnection();//创建druid链接
            DruidDataSource dds = (DruidDataSource) ds; //创建druid的数据源
            c.add(conn);//放入list中,防止被回收
            System.out.println(ds);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
            doGet( request, response);
    }
}
【web.xml配置:】
    <servlet>
        <servlet-name>ds</servlet-name>
        <servlet-class>com.suning.phl.dbcp.HoldConnection</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ds</servlet-name>
        <url-pattern>/ds/*</url-pattern>
    </servlet-mapping>
8: Publish the project to tomcat to start;
9: Access HoldConnection;
10: Access the druid console to view the data source object.
11: Create a new project and repeat steps 6-8 above;
12: Revisit the HoldConnection in the two projects (note that the paths are not the same), and then visit the druid console to view the data source object. At this time, it is found that: created Two data source objects,
it can be concluded that the data source configured in the container, accessed through different project instances will create different data source objects.
Assumption: The maximum number of connections to the database is set to 1000, and the number of connections in the container is configured to 1000. If only one instance accesses the data source, there is no problem.
Assuming that there are two instances accessing the data source, if the sum of the total number of connections of the two instances is greater than 1000, the excess part cannot be accessed to the database.

The case is as follows:

Data Source Object Study Case

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449344&siteId=291194637