Poison


  • 首页

  • 归档

  • 标签

  • 搜索
close
Poison

关于 PreparedStatement 的缓存问题

发表于 2021-09-26

之前查看 MyBatis 的代码时,发现默认使用的执行器类型为 SIMPLE,其源码位于 SimpleExecutor.java,而还有一种执行器类型为 REUSE,其源码位于 ReuseExecutor.java,如果比较源码可以发现主要的不同为 ReuseExecutor 含有如下的 HashMap 用于缓存之前创建的 PreparedStatement:

1
private final Map<String, Statement> statementMap = new HashMap<>();
阅读全文 »
Poison

SQL Injection

发表于 2021-09-24

关于 SQL 注入的防范,个人认为最可靠的方式还是使用 PreparedStatement,对于替换表名、列名等元数据字符串的场景,使用白名单对填入的值进行严格控制再传入。

在 MyBatis 的 官方文档 中,就对 ${} 的使用进行了如下提示:

It’s not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.

References

PreparedStatement
如何避免出现SQL注入漏洞
SQL Injection Prevention - OWASP Cheat Sheet Series
mybatis – MyBatis 3 | Mapper XML Files

Poison

关于分布式调用中的超时时间问题

发表于 2021-09-23

在之前处理线上事故的经验中,我发现不少问题是由于开发未设置远程调用的超时时间或者设置的超时时间较长在服务不稳定时导致线程堆积,最终导致有界线程池被打满,触发拒绝策略,影响线上服务可用性,解决方案还是尽可能的设置可接受的超时时间,而不是一味的使用默认配置或者很大的超时时间。部分开发为了自己负责的业务可用,将全局的超时时间调大,就为后续线上服务的稳定性埋下了隐患,原本在低超时时间设定下可以快速失败的请求,会长时间占用线程直到超时,导致线程堆积,服务不可用。

References

我在系统设计上犯过的14个错
Threads stuck in java.net.SocketInputStream.socketRead0 – Fast thread

Poison

Generics

发表于 2021-09-23
Generics and Subtyping

先看如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
package me.tianshuang;

import java.util.ArrayList;
import java.util.List;

public class GenericsTest {

public static void main(String[] args) {
List<String> ls = new ArrayList<>();
List<Object> lo = ls;
}

}
阅读全文 »
Poison

Should Helper/Utility Classes Be Abstract?

发表于 2021-09-01

今天看到同事写的代码中将工具类声明为 abstract,猜测是为了防止将该工具类实例化?个人认为这种方法属于使用不当,如果查看 JDK 中 Arrays 的源码:

1
2
3
4
5
6
public class Arrays {
// Suppresses default constructor, ensuring non-instantiability.
private Arrays() {}

// ...
}

可以看出,工具类 Arrays 采用了一个私有的构造函数以防止该类被实例化,同理,观察 JDK 中 Collections 的源码:

1
2
3
4
5
6
7
public class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}

// ...
}
阅读全文 »
1…161718…27

131 日志
119 标签
GitHub LeetCode
© 2025 Poison 蜀ICP备16000644号
由 Hexo 强力驱动
主题 - NexT.Mist