简体   繁体   English

如何实现数据库并发?

[英]How to achieve DB concurrency?

I have controller ,service and dao class singleton 我有控制器,服务和dao类单例

Dao Class: 道课:

@Autowired
JdbcTemplate jdbcTemplate;

    @Override

    public String addUsers(UserDTO userDto) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("JDBC TEMPLATE::"+jdbcTemplate);
        String query="Insert into users values('"+userDto.getUserName()+"')";
        System.out.println(query);
    jdbcTemplate.update(query);
    return "success";
    }

applicationContext.xml applicationContext.xml

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource" />
    </bean>



 <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"  >

       <property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/demo" />
        <property name="username" value="" /> 
       <property name="password" value="" />
    </bean>

In dao class I am using JdbcTemplate which is defined as singleton and dataSource bean is also singleton. 在dao类中,我使用的JdbcTemplate定义为单例,而dataSource bean也是单例。

Now I have following doubt: 现在我有以下疑问:

1)If my JdbcTemplate is singleton and dataSource bean is singleton will they cause any problem for concurrent request? 1)如果我的JdbcTemplate是singleton,而dataSource bean是singleton,它们会引起并发请求的任何问题吗?

2)Is that the ideal way to make JdbcTemplate bean and injecting in to DAo? 2)这是制作JdbcTemplate bean和注入DAo的理想方法吗?

3)Is request scope should only be there if any class hold instance variables? 3)仅当任何类持有实例变量时,请求范围才应该存在吗?

In order be able to work concurrently against your DB, I would suggest to you to use connection pooling. 为了能够针对您的数据库并发工作,我建议您使用连接池。

When multiple requests will "arrive" concurrently, for each of them the connections pool will assign a dedicated connection to work against the DB. 当多个请求同时“到达”时,连接池将为每个请求分配一个专用连接,以针对数据库工作。

Of course, that is under your responsibility to make sure you're not accessing to the same "area" in your DB. 当然,这是您的责任,以确保您没有访问数据库中的相同“区域”。

In MySql DB I know that there's a locking mechanism for such scenarios, but I would recommend making a deeper research. 在MySql DB中,我知道有针对这种情况的锁定机制,但我建议进行更深入的研究。

There are 2 well-known connection pools: 有2个著名的连接池:

  1. Apache DBCP http://commons.apache.org/proper/commons-dbcp/ Apache DBCP http://commons.apache.org/proper/commons-dbcp/
  2. c3po http://www.mchange.com/projects/c3p0/ c3po http://www.mchange.com/projects/c3p0/

More detailed explanation: 更详细的解释:

Connection Pooling 连接池

It's a technique to allow multiple clients to make use of a cached set of shared and reusable connection objects providing access to a database. 这是一种允许多个客户端使用一组缓存的共享和可重用连接对象的技术,从而提供对数据库的访问。

Opening/Closing database connections is an expensive process and hence connection pools improve the performance of execution of commands on a database for which we maintain connection objects in the pool. 打开/关闭数据库连接是一个昂贵的过程,因此连接池提高了数据库上命令执行的性能,为此我们在池中维护连接对象。

It facilitates reuse of the same connection object to serve a number of client requests. 它有助于重用同一连接对象来满足许多客户端请求。

Every time a client request is received, the pool is searched for an available connection object and it's highly likely that it gets a free connection object. 每次收到客户端请求时,都会在池中搜索可用的连接对象,并且很有可能获得一个空闲的连接对象。

Otherwise, either the incoming requests are queued or a new connection object is created and added to the pool (depending on how many connections are already there in the pool and how many the particular implementation and configuration can support). 否则,要么将传入的请求放入队列中,要么创建一个新的连接对象并将其添加到池中(取决于池中已经有多少个连接以及特定的实现和配置可以支持多少个连接)。

As soon as a request finishes using a connection object, the object is given back to the pool from where it's assigned to one of the queued requests (based on what scheduling algorithm the particular connection pool implementation follows for serving queued requests). 一旦使用连接对象完成请求,该对象就会被分配给其中一个排队的请求(根据特定的连接池实现服务于排队的请求的调度算法),该对象将从该池中分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM