文章目录
Could not get JDBC Connection
现象:插入数据库出现以下异常:
2019-06-05 19:15:33.414 WARN 16296 --- [http-nio-8000-exec-23] o.s.b.a.h.DataSourceHealthIndicator : Health check failed
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.jdbc.pool.PoolExhaustedException: [http-nio-8000-exec-23] Timeout: Pool empty. Unable to fetch a connection in 60 seconds, none available[size:20; busy:20; idle:0; lastwait:60000].
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:342)
at org.springframework.boot.actuate.health.DataSourceHealthIndicator.getProduct(DataSourceHealthIndicator.java:123)
原因:大量线程并发,每个线程执行大量记录的循环单笔插入,导致连接池耗尽,spring boot actuate模块获取不到连接,健康监测失败
解决:每个线程循环单笔插入的记录数量不要太多
ORA-01745: invalid host/bind variable name
现象:使用mybatis批量插入数据库出现以下异常:
### Cause: java.sql.SQLSyntaxErrorException: ORA-01745: invalid host/bind variable name
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-01745: invalid host/bind variable name
原因:最后形成的SQL语句太长
解决:批量操作数据时,控制每一次的数据量已形成合适长度的SQL语句
空字符串
varchar和varchar2的处理好像不一样,这里只考虑varchar2。
建立一个表,并插入三条记录:
create table lyy_test(a varchar2(50), b int);
insert into lyy_test(a, b) values ('', 10); //有空字符串
insert into lyy_test(a, b) values (' ', 20); //只有一个空格
insert into lyy_test(a, b) values (null, 30); //直接是null
接着查询:
select * from lyy_test; //结果有三条记录
select * from lyy_test where a = ''; //竟然没有记录!!!空字符串的记录消失了!!!
select * from lyy_test where a = ' '; //有一条记录,与插入的只有一个空格的记录相符
select * from lyy_test where a = null; //竟然没有记录!!!null的记录消失了!!!
select * from lyy_test where a is null; //竟然有两条记录
结论:
- Oralce自动把空字符串转换成null
- 只有空格的字符串没有影响
- null值的比较不能用“=”,而要用“is null”
null值比较
建立一个表,并插入一条记录:
create table lyy_test(a int, b varchar2(10), c varchar2(10));
insert into lyy_test values(1, null, null); //b和c的值都为null
select * from lyy_test; //插入的记录存在
select * from lyy_test where b = c; //使用等号比较null,看不到插入的记录
select * from lyy_test where nvl(b, 'X') = nvl(c, 'X'); //采用nvl函数
结论:
- 两个null值的比较不能用“=”,可以用nvl函数
用条件is not null查询,结果却显示null
原因很简单,就是该字段存储了字符串“null”,无语!!!!
根本原因在于如下代码:
obj1.setSomeField(String.valueOf(obj2)); //当obj2 == null时,String.valueOf方法竟然返回字符串“null”