oracle tomcat-jdbc池连接始终使用相同的会话

时间:2017-05-30 11:24:27

标签: java oracle tomcat jdbc pool

我有一个spring boot tomcat-jdbc配置:

        DataSource ds = new DataSource();

    ds.setDriverClassName("oracle.jdbc.OracleDriver");
    ds.setUrl("jdbc:oracle:thin:@10.101.7.16:1521:SIAMA");
    ds.setUsername("xxx");
    ds.setPassword("ccc");
    ds.setInitialSize(10);
    ds.setMaxActive(15);
    ds.setMaxIdle(10);
    ds.setMinIdle(5);

当我启动tomcat时,我在数据库会话中看到10个初始非活动会话。 问题是当用户启动应用程序时始终在数据库中使用相同的连接。其他9人从不使用。

这是我的代码:

package com.csi_ti.itaca.custom.general.server.service;

import org.apache.tomcat.jdbc.pool.DataSource;



public class GeneralBusinessServiceImpl implements GeneralBusinessService {

public Connection conn;
ConversionUtil convert = new ConversionUtil();

@Autowired
@Qualifier("plsqlDataSource")
private DataSource plsqlDataSource;

@PostConstruct
public void init() throws SQLException {
    System.out.println(">>>>> GeneralBusinessService Con 1");
    conn = plsqlDataSource.getConnection();
}

public Connection obtenerConexion() {
    System.out.println("Obtener conexion......................");
    try {
        if ( conn.isClosed()) {
            System.out.println(">>>>>>>>>>> Conexión cerrada");

            return conn = plsqlDataSource.getConnection();
        }
        else {
            System.out.println(">>>>>>>>>>> Conexión ABIERTA");
            conn.close();
            conn = null;
            System.out.println(">>>>>>>>>>> La cerramos");
            conn = plsqlDataSource.getConnection();
            System.out.println(">>>>>>>>>>> La volvemos a abrir " + conn.toString());
            return conn;



        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return conn;
}

public void ejecutarConsulta() {
    //System.out.println("Entramos a ejecutar consulta <<<<<<<<<<<<<<<<<<<*<*<*<*<*<*<*<*<*<*>*>*<*<" );
    //System.out.println("Antes de Pob ****: " + conn);
    System.out.println(">>>>> GeneralBusinessService Con 2");
    PAC_SHWEB_PROVEEDORES llamada = new PAC_SHWEB_PROVEEDORES(conn);
    try {
        llamada.ejecutaPAC_SHWEB_PROVEEDORES__F_LISTA_TELEFONOS_EXPEDIENTE(new BigDecimal("906000060"));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //System.out.println("Pob ****: " + llamada.toString());

}

private Generico0DTO getDTO(Object obj){

    System.out.println(">>>>> GeneralBusinessService Con 3");
    Generico0DTO dto = new Generico0DTO();
    List<Generico0.MapObject> listMapObjects= new ArrayList<Generico0.MapObject>();

    if (obj!=null){

        if (obj instanceof List<?>){
            for (Map m :(List<Map>)obj){
                Generico0.MapObject mapObject = new Generico0.MapObject();
                mapObject.setMap(m);
                listMapObjects.add(mapObject);
            }
        }else if (obj instanceof Map){

            Generico0.MapObject mapObject = new Generico0.MapObject();
            mapObject.setMap((Map)obj);
            listMapObjects.add(mapObject);

        }else if (obj instanceof BigDecimal){

            Generico0.MapObject mapObject = new Generico0.MapObject();
            Map map = new HashMap<String,BigDecimal>();
            map.put("RETURN",obj);
            mapObject.setMap(map);
            listMapObjects.add(mapObject);
        }           
    }
    dto.setMapObject(listMapObjects);
    return dto;
}

@Override
public Generico0 ejecutaPAC(String pac, String function, boolean tratarMensajes, Object... parameters) {
    System.out.println(">>>>> GeneralBusinessService Con 4");
    Map map;
    try {


        Class<?> clazz = Class.forName("com.csi_ti.itaca.custom.general.server.jdbc." + pac);
        Constructor<?> constructor =  clazz.getConstructor(Connection.class);
        Object pacInstance = constructor.newInstance(conn);

        String methodName = "ejecuta"+pac+"__"+function;

        ////System.out.println("LLamada pac: "+methodName);


        Class<?>[] parameterTypes = new Class<?>[parameters.length];
        Object[] parameterInput = new Object [parameters.length];

        for (int i = 0; i < parameters.length; i++) {
            if (parameters[i].getClass().equals(Integer.class)) {
                parameterTypes[i] = BigDecimal.class;
                parameterInput[i] = new BigDecimal((Integer) parameters[i]);
            } else {
                parameterTypes[i] = parameters[i].getClass();
                parameterInput[i] = parameters[i];
            }
        }

        Method method = clazz.getDeclaredMethod(methodName, parameterTypes);

        map = (Map) method.invoke(pacInstance, parameterInput);

        if(tratarMensajes){
            Object obj = Util.tratarRETURNyMENSAJES(map);
            return getDTO(obj);
        } else {
            return getDTO(map);
        }


    } catch (Exception e) {
        //System.out.println("Error_Service:"+e);

        return null;
    }
}

}

我需要池来平衡每个请求的会话。 有帮助吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

您的@PostConstruct方法是错误的。您可以在需要使用数据源时从数据源获取连接,而不是在post构造中初始化Connection conn变量。

try(Connection con = plsqlDataSource.getConnection()) {
    // Do something with con
}

它比你所写的要简单得多!

您的obtenerConexion()也是不必要的。您正在使用连接池,但您没有写一个连接池。

您也不应该直接使用org.apache.tomcat.jdbc.pool.DataSource,而是javax.sql.DataSource