Java笔记-JDBC心跳检测及断开重连线程实例

这里自己写了个JDBC心跳检测及断开重连线程:

运行截图如下:

心跳的时候运行ipconfig /release

然后再给他ipconfig /renew回去:

 关键代码如下:

Maven:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>18.6.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

ThreadConfig.java

@Configuration
public class ThreadConfig {

    @Bean
    public HeartThread heartThread(){

        HeartThread heartThread = new HeartThread();
        heartThread.setHeartSqlCmd("select * from v$version");
        return heartThread;
    }
}

JDBCService.java

@Service
public class JDBCService {

    static String urlString = "jdbc:oracle:thin:@//192.168.1.102:1521/orcl";

    static String userName = "xxxxxx";
    static String passWord = "xxxxxx";

    private static Connection connection = null;
    private static Statement stmt = null;

    static {
        
        try {

            //连接
            connection = DriverManager.getConnection(urlString, userName, passWord);
            stmt = connection.createStatement();
        } catch (SQLException e) {

            //记录日志
            e.printStackTrace();
        }
    }

    public static void reConnection(){
        
        try {

            //连接
            connection = DriverManager.getConnection(urlString, userName, passWord);
            stmt = connection.createStatement();
        } catch (SQLException e) {

            //记录日志
            e.printStackTrace();
        }
    }

    public void executeSQLCmd(String sql) throws SQLException, StrException {

        if(connection == null){

            throw new StrException("connection为null,赶紧退出");
        }

        if(stmt == null){

            throw new StrException("stmt为null,赶紧退出");
        }

        ResultSet rs = stmt.executeQuery(sql);
        if(rs == null){

        }
        else{

            rs.close();
        }
    }
}

HeartThread.java

public class HeartThread extends Thread {

    @Autowired
    JDBCService jdbcService;

    private String heartSqlCmd = null;

    public void setHeartSqlCmd(String heartSqlCmd) {

        this.heartSqlCmd = heartSqlCmd;
    }

    @SneakyThrows
    @Override
    public void run() {

        while (true){

            try{

                jdbcService.executeSQLCmd(heartSqlCmd);
                System.out.println(Thread.currentThread() + ":" + "连接正常");
            }
            catch (Exception e){

                //e.printStackTrace();
                //记录日志
                //重连
                System.out.println(Thread.currentThread() + ":" + "心跳检测断开正在重连");
                JDBCService.reConnection();
            }
            Thread.sleep(1000 );
        }
    }
}

 

源码打包下载地址如下:

https://github.com/fengfanchen/Java/tree/master/heartLink

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页