工作中碰到的一些错误

Feign方面的错误
1、cannot be found on object of type ‘org.springframework.cache.interceptor.CacheExpressionRootObject’
这个问题是使用@Cacheable注解时key的值没有加单引号导致的,正确用法
@Cacheable(value=“users”, key="‘users’")

2、Method has too many Body parameters
@RequestMapping(value="/operator", method=RequestMethod.GET)
Model test(@RequestParam(“name”) String name,@RequestParam(“age”) int age);
主要是没有加@RequestParam导致的

@RequestMapping(value="/operator/open", method=RequestMethod.POST)
public int save(@RequestBody Person p, @RequestBody UserModel user);
feign中你可以有多个@RequestParam,但只能有不超过一个@RequestBody。

3、今天,访问服务器接口突然报错,日志com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed
网上查了下,说是mysql默认的超时时间过了,会自动断开连接,但连接池不知道,因此使用时报错。可以通过设置来检查连接








运行show variables like ‘%timeout%’查看超时时间
spring.datasource.connection-test-query指定校验连接合法性执行的sql语句
spring.datasource.connection-timeout指定连接的超时时间,毫秒单位.
spring.datasource.idle-timeout指定连接多久没被使用时,被设置为空闲,默认为10ms
spring.datasource.initial-size指定启动连接池时,初始建立的连接数量
spring.datasource.login-timeout指定连接数据库的超时时间.

spring.datasource.max-active指定连接池中最大的活跃连接数.

spring.datasource.max-age指定连接池中连接的最大年龄

spring.datasource.max-idle指定连接池最大的空闲连接数量.

spring.datasource.max-lifetime指定连接池中连接的最大生存时间,毫秒单位.
spring.datasource.max-wait指定连接池等待连接返回的最大等待时间,毫秒单位.

spring.datasource.maximum-pool-size指定连接池最大的连接数,包括使用中的和空闲的连接.

spring.datasource.min-evictable-idle-time-millis指定一个空闲连接最少空闲多久后可被清除.

spring.datasource.min-idle指定必须保持连接的最小值(For DBCP and Tomcat connection pools)
spring.datasource.remove-abandoned指定当连接超过废弃超时时间时,是否立刻删除该连接.

spring.datasource.remove-abandoned-timeout指定连接应该被废弃的时间.
spring.datasource.test-on-borrow当从连接池借用连接时,是否测试该连接.
spring.datasource.test-on-connect创建时,是否测试连接
spring.datasource.test-on-return在连接归还到连接池时是否测试该连接.
spring.datasource.test-while-idle当连接空闲时,是否执行连接测试.
spring.datasource.time-between-eviction-runs-millis指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔

网上推荐的策略
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis= 3600000

4、java的基本类型相互间的转换
int 类型是默认的类型, byte 转int,直接赋值就可以了
byte b = 10;
int a = b;

short转int, 也是直接赋值就可以了
short a = 10;
int b = a;

int转byte/short需要强制转换,超出范围,结果是对不上的
int a = 100000;
byte b = (byte) a;
short c = (short) a;

int转long, 直接赋值就可
int a = 100
long b = a

转换的原因是byte 1个字节,short 是2个字节,int 是4个字节long是8个字节
低字节的转高字节的,肯定可以存储,高字节转低字节,如果超出了低字节的表示范围,就会数据无法表示。

int转double, 可以直接赋值,long 也可以直接赋值给double

int a = 100
double b = a;

double转int 或者long
如果在低字节的范围内,可以直接强转
double m = 10000
int b = (int) m;
long k = (long) m;

猜你喜欢

转载自blog.csdn.net/zhanglinlove/article/details/85345600