使用Java在多线程环境中访问数据库

时间:2019-06-05 12:09:51

标签: java multithreading oop spring-jdbc

我正在尝试了解在多线程环境中访问数据库的方法。

我实现了以下春季示例,该示例在多个线程中从数据库查询对象,它可以正常工作,但是我不确定这是否是正确的方法。

database.xml文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1522/orcl.168.0.106" />
        <property name="username" value="system" />
        <property name="password" value="admin" />
  </bean>

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

</beans>

UserDetails对象

package com.dataReader;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class UserDetails implements RowMapper {

    private String user_name;
    private String password;
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String username) {
        this.user_name = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        UserDetails userDetails = new UserDetails();
        userDetails.setUser_name(rs.getString("USER_NAME"));
        userDetails.setPassword(rs.getString("PASSWORD"));
        return userDetails;
    }



}

Application类如下

package com.dataReader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class Application {

    static JdbcTemplate oracleJDBCTemplate;

    public static void main(String[] args) {
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("database.xml");
        oracleJDBCTemplate = (JdbcTemplate) context.getBean("oracleJDBCTemplate");

        List<String> userList = new ArrayList<String>();
        userList.add("system");
        userList.add("user-1");
        userList.add("user-2");
        userList.add("user-10");
        userList.add("user-12");
        for(int i=0;i<userList.size();i++) {
            RunnerData runnerData = new RunnerData(oracleJDBCTemplate,userList.get(i));
            Thread thread = new Thread(runnerData);
            thread.start();
        }

    }


}

class RunnerData implements Runnable{
    private JdbcTemplate oracleJDBCTemplate;
    private String username;

    RunnerData(JdbcTemplate jdbcTemplate,String username){
        this.oracleJDBCTemplate=jdbcTemplate;
        this.username=username;
    }

    @Override
    public void run() {
            UserDetails userDetails=    oracleJDBCTemplate.queryForObject("select user_name,password from User_details where user_name=?",
                    new Object[] {username},BeanPropertyRowMapper.newInstance(UserDetails.class));
            System.out.println(userDetails.getUser_name()+" "+userDetails.getPassword());

    }

}

在多线程环境中(尤其是在我们有大量线程访问数据库的情况下)访问数据库是否正确? JdbcTemplate在查询对象时不会锁定数据库。您在设计和实施这种方案方面有什么经验?

感谢您对此的投入。

1 个答案:

答案 0 :(得分:0)

您将使用数据库连接池。 从OJDBC库使用Oracle连接池是一种方法,例如:

<bean id="datasource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
   <property name="connectionCachingEnabled" value="true" />
   <property name="url" value="jdbc:oracle:thin:@localhost:1522/orcl.168.0.106" />
   <property name="username" value="system" />
   <property name="password" value="admin" />
   <property name="connectionCacheProperties">
      <props merge="default">
         <prop key="MinLimit>5</prop>
         <prop key="MaxLimit">100</prop>
      </props>
   </property>
</bean>