Java Code Examples for com.alibaba.druid.pool.DruidPooledConnection

The following examples show how to use com.alibaba.druid.pool.DruidPooledConnection. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: nightwatch   Source File: DBUtils.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
public static DruidPooledConnection getConnection() {
    DruidPooledConnection con = null;
    try {
        con = dataSource.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return con;
}
 
Example 2
Source Project: framework   Source File: SqlLogFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author yang.zhipeng <br>
 * @taskId <br>
 * @param chain <br>
 * @param conn <br>
 * @throws SQLException <br>
 */
@Override
public void dataSource_releaseConnection(final FilterChain chain, final DruidPooledConnection conn)
    throws SQLException {
    long connectionId = -1;

    if (conn.getConnectionHolder() != null) {
        ConnectionProxy connection = (ConnectionProxy) conn.getConnectionHolder().getConnection();
        connectionId = connection.getId();
    }

    chain.dataSource_recycle(conn);
    connectionLog("{conn-" + connectionId + "} pool-recycle");
}
 
Example 3
Source Project: coming   Source File: PGValidConnectionChecker_s.java    License: MIT License 5 votes vote down vote up
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (validateQuery == null || validateQuery.isEmpty()) {
        validateQuery = this.defaultValidateQuery;
    }

    if (conn.isClosed()) {
        return false;
    }

    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
    }

    if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
    }

    int queryTimeout = validationQueryTimeout <= 0 ? defaultQueryTimeout : validationQueryTimeout;

    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        stmt.setQueryTimeout(queryTimeout);
        rs = stmt.executeQuery(validateQuery);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
 
Example 4
Source Project: coming   Source File: PGValidConnectionChecker_t.java    License: MIT License 5 votes vote down vote up
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (validateQuery == null || validateQuery.isEmpty()) {
        validateQuery = this.defaultValidateQuery;
    }

    if (conn.isClosed()) {
        return false;
    }

    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
    }

    if (conn instanceof ConnectionProxy) {
        conn = ((ConnectionProxy) conn).getRawObject();
    }

    int queryTimeout = validationQueryTimeout <= 0 ? defaultQueryTimeout : validationQueryTimeout;

    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        if(queryTimeout>=0){
            //pgsql Driver 9.0以及以下版本不支持setQueryTimeout,可通过设置queryTimeout<0兼容低版本
            stmt.setQueryTimeout(queryTimeout);
        }
        rs = stmt.executeQuery(validateQuery);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
 
Example 5
Source Project: Cynthia   Source File: DbPoolConnection.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @description:return write connection
 * @date:2014-5-6 下午12:01:54
 * @version:v1.0
 * @return
 */
public DruidPooledConnection getConnection() {
	try
	{
		return ddsWrite.getConnection();
	}catch(Exception e)
	{
		e.printStackTrace();
	}
	return null;
}
 
Example 6
Source Project: Cynthia   Source File: DbPoolConnection.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @description:return read connection
 * @date:2014-5-6 下午12:02:15
 * @version:v1.0
 * @return
 */
public DruidPooledConnection getReadConnection(){
	try
	{
		return ddsread.getConnection();
	}catch(Exception e)
	{
		e.printStackTrace();
	}
	return null;
}
 
Example 7
Source Project: Autumn   Source File: SysMapperImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
    public List<SysMenu> getMenuBySys(String sys) throws SQLException {

        String sql_select_funcgrp = "SELECT * FROM af_funcgrp WHERE type = 'leftMenu' AND status = 1 AND sys = ? ORDER BY disporder";
        String sq_select_func = "SELECT * FROM af_func WHERE grp_name = ? AND status = 1 ORDER BY disporder";

        DruidPooledConnection connection = druidDataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(sql_select_funcgrp);
        statement.setString(1, sys);
        ResultSet resultSet = statement.executeQuery();
        List<SysMenu> sysMenuList = new ArrayList<>();
        while (resultSet.next()){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setMenu_name(resultSet.getString("namec"));
            sysMenu.setHref(resultSet.getString("plugin"));
            sysMenu.setIcon(resultSet.getString("icon"));

            PreparedStatement statement1 = connection.prepareStatement(sq_select_func);
            statement1.setString(1, resultSet.getString("name"));
            ResultSet resultSet1 = statement1.executeQuery();
            List<SysMenu.ChildMenu> childMenuList = new ArrayList<>();
            int i = 0;
            while (resultSet1.next()){
                SysMenu.ChildMenu childMenu = sysMenu.new ChildMenu();
                childMenu.setMenu_name(resultSet1.getString("namec"));
                childMenu.setHref(resultSet1.getString("plugin"));
//                childMenu.setIcon(resultSet1.getString("icon"));
                childMenuList.add(childMenu);
                i++;
            }
            statement1.close();
            //resultSet1.close();
            sysMenu.setChild_num(i);
            sysMenu.setChildes(childMenuList);
            sysMenuList.add(sysMenu);
        }
        connection.close();
        resultSet.close();      //  只需关闭一次
        statement.close();
        return sysMenuList;
    }
 
Example 8
Source Project: Autumn   Source File: MenuManageMapperImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
    public List<VoMenu> getMenuByPage(int page, int limit, int level, String type, String sys) throws SQLException {
//        String sql_select_menu = "SELECT id, sys, name, state, disporder, type, level, parent_name";
//        String sql_select_menu = "SELECT id, sys, name, state, disporder, type, level, parent_name";
        String sql_select_menu = "SELECT * FROM ! WHERE type = ? AND sys = ? LIMIT " + (page - 1) * limit + "," + (page * limit);

        if (level == 1){
            sql_select_menu = sql_select_menu.replace("!", "af_funcgrp");
        }else if(level == 2){
            sql_select_menu = sql_select_menu.replace("!", "af_func");
        }
        DruidPooledConnection connection = druidDataSource.getConnection();
        PreparedStatement statement = connection.prepareStatement(sql_select_menu);
        statement.setString(1, type);
        statement.setString(2, sys);
        ResultSet resultSet = statement.executeQuery();
        List<VoMenu> voMenuList = new ArrayList<>();
        while(resultSet.next()){
            VoMenu voMenu = new VoMenu();
            voMenu.setId(resultSet.getInt("id"));
            voMenu.setDisporder(resultSet.getInt("disporder"));
            voMenu.setLevel(level + "级菜单");
            voMenu.setName(resultSet.getString("name"));
            voMenu.setNamee(resultSet.getString("namee"));
            voMenu.setNamec(resultSet.getString("namec"));
            voMenu.setSys(resultSet.getString("sys"));
            voMenu.setIcon(resultSet.getString("icon"));
            if (level == 1) {
                voMenu.setParent_name("");
            }
            else if (level == 2) {
                voMenu.setParent_name(resultSet.getString("grp_name"));
            }
            if (resultSet.getInt("status") == 0){
                voMenu.setStatus(BusinessConstants.SYS_MENU_STATUS_0.getMsg());
            }else if (resultSet.getInt("status") == 1){
                voMenu.setStatus(BusinessConstants.SYS_MENU_STATUS_1.getMsg());
            }
            voMenu.setType(type);
            voMenuList.add(voMenu);
        }

        resultSet.close();
        statement.close();
        connection.close();
        return voMenuList;
    }
 
Example 9
Source Project: tddl5   Source File: EncodingUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String getEncoding(Connection conn) {
    if (conn instanceof IConnection) {
        return ((IConnection) conn).getEncoding();
    }

    if (conn instanceof TConnectionWrapper) {
        conn = ((TConnectionWrapper) conn).getTargetConnection();
        return getEncoding(conn);
    }

    if (conn instanceof DruidPooledConnection) {
        conn = ((DruidPooledConnection) conn).getConnection();
        return getEncoding(conn);
    }

    try {
        final Class<?> clazz = conn.getClass();
        Method getMethod = getMethodCaches.get(clazz, new Callable<Method>() {

            @Override
            public Method call() throws Exception {

                Class c = clazz;
                while (true) {
                    try {
                        Method setMethod = c.getDeclaredMethod("getEncoding", new Class[] {});
                        if (setMethod != null) {
                            setMethod.setAccessible(true);
                        }
                        return setMethod;
                    } catch (Exception ex) {
                    }

                    c = c.getSuperclass();

                    if (c == null) {
                        throw new TddlRuntimeException(ErrorCode.ERR_EXECUTOR, "get getEncoding error, clazz:"
                                                                               + clazz);
                    }
                }
            }
        });

        return (String) getMethod.invoke(conn, new Object[] {});
    } catch (Throwable e) {
        throw new TddlNestableRuntimeException(e);
    }
}
 
Example 10
Source Project: jframe   Source File: DruidServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void recycleConnection(Connection conn) throws SQLException {
    ((DruidPooledConnection) conn).recycle();
}
 
Example 11
Source Project: jframe   Source File: DruidServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void closeConnection(Connection conn) throws SQLException {
    ((DruidPooledConnection) conn).close();
}
 
Example 12
Source Project: pinpoint   Source File: DruidIT.java    License: Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setUp() throws NoSuchMethodException {

    getConnectionMethod = DruidDataSource.class.getDeclaredMethod("getConnection");
    closeConnectionMethod = DruidPooledConnection.class.getDeclaredMethod("close");
}
 
Example 13
Source Project: framework   Source File: SqlLogFilter.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param chain
 * @param ds
 * @param maxWaitMillis
 * @return DruidPooledConnection
 * @throws SQLException <br>
 */
@Override
public DruidPooledConnection dataSource_getConnection(final FilterChain chain, final DruidDataSource ds,
    final long maxWaitMillis) throws SQLException {
    DruidPooledConnection conn = chain.dataSource_connect(ds, maxWaitMillis);
    ConnectionProxy connection = (ConnectionProxy) conn.getConnectionHolder().getConnection();
    connectionLog("{conn-" + connection.getId() + "} pool-connect");

    return conn;
}
 
Example 14
/**
 * 返回druid数据库连接
 *
 * @return
 * @throws SQLException
 */
public DruidPooledConnection getConnection() throws SQLException {
    return druidDataSource.getConnection();
}