Просмотр исходного кода

获取数据源时增加判断,解决Druid监控中jdbc出错数为4的问题

sunyj 9 лет назад
Родитель
Сommit
71c322b890

+ 2 - 2
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -126,9 +126,9 @@ public class PrintServiceImpl implements PrintService {
 
 		Connection connection = null;
 		try {
-			logger.info("dataSource.getConnection...");
+			logger.info("dataSource.getConnection..." + userName);
 			connection = dataSource.getConnection();
-			logger.info("dataSource.getConnection done...");
+			logger.info("dataSource.getConnection done..." + userName);
 
 			Map<String, Object> result = new HashMap<>();
 			byte[] data = null;

+ 22 - 18
src/main/java/com/uas/report/util/DataSourceUtils.java

@@ -8,7 +8,6 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.SQLSyntaxErrorException;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -137,28 +136,33 @@ public class DataSourceUtils {
 		PreparedStatement preparedStatement = null;
 		ResultSet resultSet = null;
 		try {
-			logger.info("mainMaster.getConnection...");
+			logger.info("mainMaster.getConnection..." + mainMaster.getUsername() + "," + userName);
 			connection = mainMaster.getConnection();
-			logger.info("mainMaster.getConnection done...");
-			// 根据当前账套用户名获取其数据库配置信息
-			String sql = "select * from master where MA_USER = ?";
+			logger.info("mainMaster.getConnection done..." + mainMaster.getUsername() + "," + userName);
+			// 先检查是否存在MASTER表
+			String sql = "select count(*) from user_tables where table_name='MASTER'";
 			preparedStatement = connection.prepareStatement(sql);
-			preparedStatement.setString(1, userName);
-			try {
-				resultSet = preparedStatement.executeQuery();
-			} catch (SQLSyntaxErrorException e) {
-				// 并不是每个数据源都有MA_USER表,因此,出现异常,返回空,紧接着遍历下一个数据源
+			resultSet = preparedStatement.executeQuery();
+			resultSet.next();
+			// 并不是每个数据源都有MA_USER表,如果不存在,紧接着遍历下一个数据源
+			if (resultSet.getInt(1) == 0) {
 				return null;
 			}
-			if (resultSet.next()) {
+			preparedStatement.close();
+			resultSet.close();
+
+			// 根据当前账套用户名获取其数据库配置信息
+			sql = "select * from master where MA_USER = ?";
+			preparedStatement = connection.prepareStatement(sql);
+			preparedStatement.setString(1, userName);
+			resultSet = preparedStatement.executeQuery();
+			if (resultSet.next() && !StringUtils.isEmpty(resultSet.getString("MS_PWD"))) {
 				String password = resultSet.getString("MS_PWD");
-				if (!StringUtils.isEmpty(password)) {
-					// 除了用户名、密码,其他属性一样
-					DruidDataSource result = mainMaster.cloneDruidDataSource();
-					result.setUsername(userName);
-					result.setPassword(password);
-					return result;
-				}
+				// 除了用户名、密码,其他属性一样
+				DruidDataSource result = mainMaster.cloneDruidDataSource();
+				result.setUsername(userName);
+				result.setPassword(password);
+				return result;
 			}
 		} catch (Exception e) {
 			throw new ReportException(e).setDetailedMessage(e);