1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Java日常開發(fā)的21個坑,你踩過幾個?

        共 34899字,需瀏覽 70分鐘

         ·

        2021-03-14 14:42

        點擊關(guān)注公眾號,Java干貨及時送達

        1. 六類典型空指針問題

        • 包裝類型的空指針問題
        • 級聯(lián)調(diào)用的空指針問題
        • Equals方法左邊的空指針問題
        • ConcurrentHashMap 這樣的容器不支持 Key 和 Value 為 null。
        • 集合,數(shù)組直接獲取元素
        • 對象直接獲取屬性

        1.1包裝類型的空指針問題

        public class NullPointTest {

            public static void main(String[] args) throws InterruptedException {
                System.out.println(testInteger(null));
            }

            private static Integer testInteger(Integer i) {
                return i + 1;  //包裝類型,傳參可能為null,直接計算,則會導致空指針問題
            }
        }

        1.2 級聯(lián)調(diào)用的空指針問題

        public class NullPointTest {
            public static void main(String[] args) {
               //fruitService.getAppleService() 可能為空,會導致空指針問題
                fruitService.getAppleService().getWeight().equals("OK");
            }
        }

        1.3 Equals方法左邊的空指針問題

        public class NullPointTest {
            public static void main(String[] args) {
                String s = null;
                if (s.equals("公眾號:Java技術(shù)棧,666")) { //s可能為空,會導致空指針問題
                    System.out.println("666");
                }
            }
        }

        1.4 ConcurrentHashMap 這樣的容器不支持 Key,Value 為 null。

        public class NullPointTest {
            public static void main(String[] args) {
                Map map = new ConcurrentHashMap<>();
                String key = null;
                String value = null;
                map.put(key, value);
            }
        }

        1.5  集合,數(shù)組直接獲取元素

        public class NullPointTest {
            public static void main(String[] args) {
                int [] array=null;
                List list = null;
                System.out.println(array[0]); //空指針異常
                System.out.println(list.get(0)); //空指針一場
            }
        }

        1.6 對象直接獲取屬性

        public class NullPointTest {
            public static void main(String[] args) {
                User user=null;
                System.out.println(user.getAge()); //空指針異常
            }
        }

        2. 日期YYYY格式設(shè)置的坑

        日常開發(fā),經(jīng)常需要對日期格式化,但是呢,年份設(shè)置為YYYY大寫的時候,是有坑的哦。YYYY-MM-DD 的黑鍋,我們不背!這篇推薦看下。

        反例:

        Calendar calendar = Calendar.getInstance();
        calendar.set(2019, Calendar.DECEMBER, 31);

        Date testDate = calendar.getTime();

        SimpleDateFormat dtf = new SimpleDateFormat("YYYY-MM-dd");
        System.out.println("2019-12-31 轉(zhuǎn) YYYY-MM-dd 格式后 " + dtf.format(testDate));

        運行結(jié)果:

        2019-12-31 轉(zhuǎn) YYYY-MM-dd 格式后 2020-12-31

        「解析:」

        為什么明明是2019年12月31號,就轉(zhuǎn)了一下格式,就變成了2020年12月31號了?因為YYYY是基于周來計算年的,它指向當天所在周屬于的年份,一周從周日開始算起,周六結(jié)束,只要本周跨年,那么這一周就算下一年的了。正確姿勢是使用yyyy格式。

        正例:

        Calendar calendar = Calendar.getInstance();
        calendar.set(2019, Calendar.DECEMBER, 31);

        Date testDate = calendar.getTime();

        SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("2019-12-31 轉(zhuǎn) yyyy-MM-dd 格式后 " + dtf.format(testDate));

        3.金額數(shù)值計算精度的坑

        看下這個浮點數(shù)計算的例子吧:

        public class DoubleTest {
            public static void main(String[] args) {
                System.out.println(0.1+0.2);
                System.out.println(1.0-0.8);
                System.out.println(4.015*100);
                System.out.println(123.3/100);

                double amount1 = 3.15;
                double amount2 = 2.10;
                if (amount1 - amount2 == 1.05){
                    System.out.println("OK");
                }
            }
        }

        運行結(jié)果:

        0.30000000000000004
        0.19999999999999996
        401.49999999999994
        1.2329999999999999

        可以發(fā)現(xiàn),結(jié)算結(jié)果跟我們預期不一致,其實是因為計算機是以二進制存儲數(shù)值的,對于浮點數(shù)也是。對于計算機而言,0.1無法精確表達,這就是為什么浮點數(shù)會導致精確度缺失的。因此,「金額計算,一般都是用BigDecimal 類型」

        Java中的運算神器 BigDecimal,了解一下?

        對于以上例子,我們改為BigDecimal,再看看運行效果:

        System.out.println(new BigDecimal(0.1).add(new BigDecimal(0.2)));
        System.out.println(new BigDecimal(1.0).subtract(new BigDecimal(0.8)));
        System.out.println(new BigDecimal(4.015).multiply(new BigDecimal(100)));
        System.out.println(new BigDecimal(123.3).divide(new BigDecimal(100)));

        運行結(jié)果:

        0.3000000000000000166533453693773481063544750213623046875
        0.1999999999999999555910790149937383830547332763671875
        401.49999999999996802557689079549163579940795898437500
        1.232999999999999971578290569595992565155029296875

        發(fā)現(xiàn)結(jié)果還是不對,「其實」,使用 BigDecimal 表示和計算浮點數(shù),必須使用「字符串的構(gòu)造方法」來初始化 BigDecimal,正例如下:

        public class DoubleTest {
            public static void main(String[] args) {
                System.out.println(new BigDecimal("0.1").add(new BigDecimal("0.2")));
                System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.8")));
                System.out.println(new BigDecimal("4.015").multiply(new BigDecimal("100")));
                System.out.println(new BigDecimal("123.3").divide(new BigDecimal("100")));
            }
        }

        在進行金額計算,使用BigDecimal的時候,我們還需要「注意BigDecimal的幾位小數(shù)點,還有它的八種舍入模式哈」。

        4. FileReader默認編碼導致亂碼問題

        看下這個例子:

        public class FileReaderTest {
            public static void main(String[] args) throws IOException {

                Files.deleteIfExists(Paths.get("jay.txt"));
                Files.write(Paths.get("jay.txt"), "你好,撿田螺的小男孩".getBytes(Charset.forName("GBK")));
                System.out.println("系統(tǒng)默認編碼:"+Charset.defaultCharset());

                char[] chars = new char[10];
                String content = "";
                try (FileReader fileReader = new FileReader("jay.txt")) {
                    int count;
                    while ((count = fileReader.read(chars)) != -1) {
                        content += new String(chars, 0, count);
                    }
                }
                System.out.println(content);
            }
        }

        運行結(jié)果:

        系統(tǒng)默認編碼:UTF-8
        ???,???????С?к?

        從運行結(jié)果,可以知道,系統(tǒng)默認編碼是utf8,demo中讀取出來,出現(xiàn)亂碼了。為什么呢?

        ?

        FileReader 是以當「前機器的默認字符集」來讀取文件的,如果希望指定字符集的話,需要直接使用 InputStreamReader 和 FileInputStream。

        ?

        正例如下:

        public class FileReaderTest {
            public static void main(String[] args) throws IOException {

                Files.deleteIfExists(Paths.get("jay.txt"));
                Files.write(Paths.get("jay.txt"), "你好,撿田螺的小男孩".getBytes(Charset.forName("GBK")));
                System.out.println("系統(tǒng)默認編碼:"+Charset.defaultCharset());

                char[] chars = new char[10];
                String content = "";
                try (FileInputStream fileInputStream = new FileInputStream("jay.txt");
                     InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName("GBK"))) {
                    int count;
                    while ((count = inputStreamReader.read(chars)) != -1) {
                        content += new String(chars, 0, count);
                    }
                }
                System.out.println(content);
            }
        }

        5. Integer緩存的坑

        public class IntegerTest {

            public static void main(String[] args) {
                Integer a = 127;
                Integer b = 127;
                System.out.println("a==b:"+ (a == b));
                
                Integer c = 128;
                Integer d = 128;
                System.out.println("c==d:"+ (c == d));
            }
        }

        運行結(jié)果:

        a==b:true
        c==d:false

        為什么Integer值如果是128就不相等了呢?「編譯器會把 Integer a = 127 轉(zhuǎn)換為 Integer.valueOf(127)?!?/strong> 我們看下源碼。

        public static Integer valueOf(int i) {
              if (i >= IntegerCache.low && i <= IntegerCache.high)
                  return IntegerCache.cache[i + (-IntegerCache.low)];
              return new Integer(i);
         }

        可以發(fā)現(xiàn),i在一定范圍內(nèi),是會返回緩存的。

        ?

        默認情況下呢,這個緩存區(qū)間就是[-128, 127],所以我們業(yè)務日常開發(fā)中,如果涉及Integer值的比較,需要注意這個坑哈。還有呢,設(shè)置 JVM 參數(shù)加上 -XX:AutoBoxCacheMax=1000,是可以調(diào)整這個區(qū)間參數(shù)的,大家可以自己試一下哈

        ?

        6. static靜態(tài)變量依賴spring實例化變量,可能導致初始化出錯

        之前看到過類似的代碼。靜態(tài)變量依賴于spring容器的bean。

         private static SmsService smsService = SpringContextUtils.getBean(SmsService.class);

        這個靜態(tài)的smsService有可能獲取不到的,因為類加載順序不是確定的,正確的寫法可以這樣,如下:

         private static SmsService  smsService =null;
         
         //使用到的時候采取獲取
         public static SmsService getSmsService(){
           if(smsService==null){
              smsService = SpringContextUtils.getBean(SmsService.class);
           }
           return smsService;
         }

        7. 使用ThreadLocal,線程重用導致信息錯亂的坑

        使用ThreadLocal緩存信息,有可能出現(xiàn)信息錯亂的情況。看下下面這個例子吧。

        private static final ThreadLocal<Integer> currentUser = ThreadLocal.withInitial(() -> null);

        @GetMapping("wrong")
        public Map wrong(@RequestParam("userId") Integer userId) {
            //設(shè)置用戶信息之前先查詢一次ThreadLocal中的用戶信息
            String before  = Thread.currentThread().getName() + ":" + currentUser.get();
            //設(shè)置用戶信息到ThreadLocal
            currentUser.set(userId);
            //設(shè)置用戶信息之后再查詢一次ThreadLocal中的用戶信息
            String after  = Thread.currentThread().getName() + ":" + currentUser.get();
            //匯總輸出兩次查詢結(jié)果
            Map result = new HashMap();
            result.put("before", before);
            result.put("after", after);
            return result;
        }

        按理說,每次獲取的before應該都是null,但是呢,程序運行在 Tomcat 中,執(zhí)行程序的線程是 Tomcat 的工作線程,而 Tomcat 的工作線程是基于線程池的。

        ?

        線程池會重用固定的幾個線程,一旦線程重用,那么很可能首次從 ThreadLocal 獲取的值是之前其他用戶的請求遺留的值。這時,ThreadLocal 中的用戶信息就是其他用戶的信息。

        ?

        把tomcat的工作線程設(shè)置為1

        server.tomcat.max-threads=1

        用戶1,請求過來,會有以下結(jié)果,符合預期:

        用戶2請求過來,會有以下結(jié)果,「不符合預期」

        因此,使用類似 ThreadLocal 工具來存放一些數(shù)據(jù)時,需要特別注意在代碼運行完后,顯式地去清空設(shè)置的數(shù)據(jù),正例如下:

        @GetMapping("right")
        public Map right(@RequestParam("userId") Integer userId) {
            String before  = Thread.currentThread().getName() + ":" + currentUser.get();
            currentUser.set(userId);
            try {
                String after = Thread.currentThread().getName() + ":" + currentUser.get();
                Map result = new HashMap();
                result.put("before", before);
                result.put("after", after);
                return result;
            } finally {
                //在finally代碼塊中刪除ThreadLocal中的數(shù)據(jù),確保數(shù)據(jù)不串
                currentUser.remove();
            }
        }

        8. 疏忽switch的return和break

        這一點嚴格來說,應該不算坑,但是呢,大家寫代碼的時候,有些朋友容易疏忽了。直接看例子吧

        public class SwitchTest {

            public static void main(String[] args) throws InterruptedException {
                System.out.println("testSwitch結(jié)果是:"+testSwitch("1"));
            }

            private static String testSwitch(String key) {
                switch (key) {
                    case "1":
                        System.out.println("1");
                    case "2":
                        System.out.println(2);
                        return "2";
                    case "3":
                        System.out.println("3");
                    default:
                        System.out.println("返回默認值");
                        return "4";
                }
            }
        }

        輸出結(jié)果:

        測試switch
        1
        2
        testSwitch結(jié)果是:2

        switch 是會「沿著case一直往下匹配的,知道遇到return或者break?!?/strong> 所以,在寫代碼的時候留意一下,是不是你要的結(jié)果。

        9. Arrays.asList的幾個坑

        9.1 基本類型不能作為 Arrays.asList方法的參數(shù),否則會被當做一個參數(shù)。

        public class ArrayAsListTest {
            public static void main(String[] args) {
                int[] array = {1, 2, 3};
                List list = Arrays.asList(array);
                System.out.println(list.size());
            }
        }

        運行結(jié)果:

        1

        Arrays.asList源碼如下:

        public static <T> List<T> asList(T... a) {
            return new ArrayList<>(a);
        }

        9.2 Arrays.asList 返回的 List 不支持增刪操作。

        public class ArrayAsListTest {
            public static void main(String[] args) {
                String[] array = {"1""2""3"};
                List list = Arrays.asList(array);
                list.add("5");
                System.out.println(list.size());
            }
        }

        運行結(jié)果:

        Exception in thread "main" java.lang.UnsupportedOperationException
         at java.util.AbstractList.add(AbstractList.java:148)
         at java.util.AbstractList.add(AbstractList.java:108)
         at object.ArrayAsListTest.main(ArrayAsListTest.java:11)

        Arrays.asList 返回的 List 并不是我們期望的 java.util.ArrayList,而是 Arrays 的內(nèi)部類 ArrayList。內(nèi)部類的ArrayList沒有實現(xiàn)add方法,而是父類的add方法的實現(xiàn),是會拋出異常的呢。

        9.3 使用Arrays.asLis的時候,對原始數(shù)組的修改會影響到我們獲得的那個List

        public class ArrayAsListTest {
            public static void main(String[] args) {
                String[] arr = {"1""2""3"};
                List list = Arrays.asList(arr);
                arr[1] = "4";
                System.out.println("原始數(shù)組"+Arrays.toString(arr));
                System.out.println("list數(shù)組" + list);
            }
        }

        運行結(jié)果:

        原始數(shù)組[1, 4, 3]
        list數(shù)組[1, 4, 3]

        從運行結(jié)果可以看到,原數(shù)組改變,Arrays.asList轉(zhuǎn)化來的list也跟著改變啦,大家使用的時候要注意一下哦,可以用new ArrayList(Arrays.asList(arr))包一下的。

        10. ArrayList.toArray() 強轉(zhuǎn)的坑

        public class ArrayListTest {
            public static void main(String[] args) {
                List<String> list = new ArrayList<String>(1);
                list.add("公眾號:Java技術(shù)棧");
                String[] array21 = (String[])list.toArray();//類型轉(zhuǎn)換異常
            }
        }

        因為返回的是Object類型,Object類型數(shù)組強轉(zhuǎn)String數(shù)組,會發(fā)生ClassCastException。解決方案是,使用toArray()重載方法toArray(T[] a)

        String[] array1 = list.toArray(new String[0]);//可以正常運行

        11. 異常使用的幾個坑

        11.1 不要弄丟了你的堆棧異常信息

        public void wrong1(){
            try {
                readFile();
            } catch (IOException e) {
                //沒有把異常e取出來,原始異常信息丟失  
                throw new RuntimeException("系統(tǒng)忙請稍后再試");
            }
        }

        public void wrong2(){
            try {
                readFile();
            } catch (IOException e) {
                //只保留了異常消息,棧沒有記錄啦
                log.error("文件讀取錯誤, {}", e.getMessage());
                throw new RuntimeException("系統(tǒng)忙請稍后再試");
            }
        }

        正確的打印方式,應該醬紫

        public void right(){
            try {
                readFile();
            } catch (IOException e) {
                //把整個IO異常都記錄下來,而不是只打印消息
                log.error("文件讀取錯誤", e);
                throw new RuntimeException("系統(tǒng)忙請稍后再試");
            }
        }

        11.2 不要把異常定義為靜態(tài)變量

        public void testStaticExeceptionOne{
            try {
                exceptionOne();
            } catch (Exception ex) {
                log.error("exception one error", ex);
            }
            try {
                exceptionTwo();
            } catch (Exception ex) {
                log.error("exception two error", ex);
            }
        }

        private void exceptionOne() {
            //這里有問題
            throw Exceptions.ONEORTWO;
        }

        private void exceptionTwo() {
            //這里有問題
            throw Exceptions.ONEORTWO;
        }

        exceptionTwo拋出的異常,很可能是 exceptionOne的異常哦。正確使用方法,應該是new 一個出來。

        private void exceptionTwo() {
            throw new BusinessException("業(yè)務異常", 0001);
        }

        11.3 生產(chǎn)環(huán)境不要使用e.printStackTrace();

        public void wrong(){
            try {
                readFile();
            } catch (IOException e) {
               //生產(chǎn)環(huán)境別用它
                e.printStackTrace();
            }
        }

        因為它占用太多內(nèi)存,造成鎖死,并且,日志交錯混合,也不易讀。正確使用如下:

        log.error("異常日志正常打印方式",e);

        11.4 線程池提交過程中,出現(xiàn)異常怎么辦?

        public class ThreadExceptionTest {

            public static void main(String[] args) {
                ExecutorService executorService = Executors.newFixedThreadPool(10);

                IntStream.rangeClosed(1, 10).forEach(i -> executorService.submit(()-> {
                            if (i == 5) {
                                System.out.println("發(fā)生異常啦");
                                throw new RuntimeException("error");
                            }
                            System.out.println("當前執(zhí)行第幾:" + Thread.currentThread().getName() );
                        }
                ));
                executorService.shutdown();
            }
        }

        運行結(jié)果:

        當前執(zhí)行第幾:pool-1-thread-1
        當前執(zhí)行第幾:pool-1-thread-2
        當前執(zhí)行第幾:pool-1-thread-3
        當前執(zhí)行第幾:pool-1-thread-4
        發(fā)生異常啦
        當前執(zhí)行第幾:pool-1-thread-6
        當前執(zhí)行第幾:pool-1-thread-7
        當前執(zhí)行第幾:pool-1-thread-8
        當前執(zhí)行第幾:pool-1-thread-9
        當前執(zhí)行第幾:pool-1-thread-10

        可以發(fā)現(xiàn),如果是使用submit方法提交到線程池的異步任務,異常會被吞掉的,所以在日常發(fā)現(xiàn)中,如果會有可預見的異常,可以采取這幾種方案處理:

        • 1.在任務代碼try/catch捕獲異常
        • 2.通過Future對象的get方法接收拋出的異常,再處理
        • 3.為工作者線程設(shè)置UncaughtExceptionHandler,在uncaughtException方法中處理異常
        • 4.重寫ThreadPoolExecutor的afterExecute方法,處理傳遞的異常引用

        11.5 finally重新拋出的異常也要注意啦

        public void wrong() {
            try {
                log.info("try");
                //異常丟失
                throw new RuntimeException("try");
            } finally {
                log.info("finally");
                throw new RuntimeException("finally");
            }
        }

        一個方法是不會出現(xiàn)兩個異常的呢,所以finally的異常會把try的「異常覆蓋」。正確的使用方式應該是,finally 代碼塊「負責自己的異常捕獲和處理」

        public void right() {
            try {
                log.info("try");
                throw new RuntimeException("try");
            } finally {
                log.info("finally");
                try {
                    throw new RuntimeException("finally");
                } catch (Exception ex) {
                    log.error("finally", ex);
                }
            }
        }

        12.JSON序列化,Long類型被轉(zhuǎn)成Integer類型!

        public class JSONTest {
            public static void main(String[] args) {

                Long idValue = 3000L;
                Map<String, Object> data = new HashMap<>(2);
                data.put("id", idValue);
                data.put("name""撿田螺的小男孩");

                Assert.assertEquals(idValue, (Long) data.get("id"));
                String jsonString = JSON.toJSONString(data);

                // 反序列化時Long被轉(zhuǎn)為了Integer
                Map map = JSON.parseObject(jsonString, Map.class);
                Object idObj = map.get("id");
                System.out.println("反序列化的類型是否為Integer:"+(idObj instanceof Integer));
                Assert.assertEquals(idValue, (Long) idObj);
            }
        }

        「運行結(jié)果:」

        Exception in thread "main" 反序列化的類型是否為Integer:true
        java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
         at object.JSONTest.main(JSONTest.java:24)
        ?

        「注意啦」,序列化為Json串后,Josn串是沒有Long類型呢。而且反序列化回來如果也是Object接收,數(shù)字小于Interger最大值的話,給轉(zhuǎn)成Integer啦!

        ?

        13. 使用Executors聲明線程池,newFixedThreadPool的OOM問題

        ExecutorService executor = Executors.newFixedThreadPool(10);
                for (int i = 0; i < Integer.MAX_VALUE; i++) {
                    executor.execute(() -> {
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            //do nothing
                        }
                    });
                }

        「IDE指定JVM參數(shù):-Xmx8m -Xms8m :」

        運行結(jié)果:

        我們看下源碼,其實newFixedThreadPool使用的是無界隊列!

        public static ExecutorService newFixedThreadPool(int nThreads) {
            return new ThreadPoolExecutor(nThreads, nThreads,
                                          0L, TimeUnit.MILLISECONDS,
                                          new LinkedBlockingQueue<Runnable>());
        }

        public class LinkedBlockingQueue<E> extends AbstractQueue<E>
                implements BlockingQueue<E>, java.io.Serializable {
            ...


            /**
             * Creates a {@code LinkedBlockingQueue} with a capacity of
             * {@link Integer#MAX_VALUE}.
             */
            public LinkedBlockingQueue() {
                this(Integer.MAX_VALUE);
            }
        ...
        }
        ?

        newFixedThreadPool線程池的核心線程數(shù)是固定的,它使用了近乎于無界的LinkedBlockingQueue阻塞隊列。當核心線程用完后,任務會入隊到阻塞隊列,如果任務執(zhí)行的時間比較長,沒有釋放,會導致越來越多的任務堆積到阻塞隊列,最后導致機器的內(nèi)存使用不停的飆升,造成JVM OOM。

        ?

        14. 直接大文件或者一次性從數(shù)據(jù)庫讀取太多數(shù)據(jù)到內(nèi)存,可能導致OOM問題

        如果一次性把大文件或者數(shù)據(jù)庫太多數(shù)據(jù)達到內(nèi)存,是會導致OOM的。所以,為什么查詢DB數(shù)據(jù)庫,一般都建議分批。

        讀取文件的話,一般問文件不會太大,才使用Files.readAllLines()。為什么呢?因為它是直接把文件都讀到內(nèi)存的,預估下不會OOM才使用這個吧,可以看下它的源碼:

        public static List<String> readAllLines(Path path, Charset cs) throws IOException {
            try (BufferedReader reader = newBufferedReader(path, cs)) {
                List<String> result = new ArrayList<>();
                for (;;) {
                    String line = reader.readLine();
                    if (line == null)
                        break;
                    result.add(line);
                }
                return result;
            }
        }

        如果是太大的文件,可以使用Files.line()按需讀取,當時讀取文件這些,一般是使用完需要「關(guān)閉資源流」的哈

        15. 先查詢,再更新/刪除的并發(fā)一致性問題

        再日常開發(fā)中,這種代碼實現(xiàn)經(jīng)??梢姡合炔樵兪欠裼惺S嗫捎玫钠?,再去更新票余量。

        if(selectIsAvailable(ticketId){ 
            1、deleteTicketById(ticketId) 
            2、給現(xiàn)金增加操作 
        }else
            return “沒有可用現(xiàn)金券” 
        }

        如果是并發(fā)執(zhí)行,很可能有問題的,應該利用數(shù)據(jù)庫的更新/刪除的原子性,正解如下:

        if(deleteAvailableTicketById(ticketId) == 1){ 
            1、給現(xiàn)金增加操作 
        }else
            return “沒有可用現(xiàn)金券” 
        }

        16. 數(shù)據(jù)庫使用utf-8存儲, 插入表情異常的坑

        低版本的MySQL支持的utf8編碼,最大字符長度為 3 字節(jié),但是呢,存儲表情需要4個字節(jié),因此如果用utf8存儲表情的話,會報SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column,所以一般用utf8mb4編碼去存儲表情。

        17. 事務未生效的坑

        日常業(yè)務開發(fā)中,我們經(jīng)常跟事務打交道,「事務失效」主要有以下幾個場景:

        • 底層數(shù)據(jù)庫引擎不支持事務
        • 在非public修飾的方法使用
        • rollbackFor屬性設(shè)置錯誤
        • 本類方法直接調(diào)用
        • 異常被try...catch吃了,導致事務失效。

        其中,最容易踩的坑就是后面兩個,「注解的事務方法給本類方法直接調(diào)用」,偽代碼如下:

        public class TransactionTest{
          public void A(){
            //插入一條數(shù)據(jù)
            //調(diào)用方法B (本地的類調(diào)用,事務失效了)
            B();
          }
          
          @Transactional
          public void B(){
            //插入數(shù)據(jù)
          }
        }

        如果異常被catch住,「那事務也是會失效呢」~,偽代碼如下:

        @Transactional
        public void method(){
          try{
            //插入一條數(shù)據(jù)
            insertA();
            //更改一條數(shù)據(jù)
            updateB();
          }catch(Exception e){
            logger.error("異常被捕獲了,那你的事務就失效咯",e);
          }
        }

        18. 當反射遇到方法重載的坑

        /**
         *  反射demo
         *  @author 撿田螺的小男孩
         */
        public class ReflectionTest {

            private void score(int score) {
                System.out.println("int grade =" + score);
            }

            private void score(Integer score) {
                System.out.println("Integer grade =" + score);
            }

            public static void main(String[] args) throws Exception {
                ReflectionTest reflectionTest = new ReflectionTest();
                reflectionTest.score(100);
                reflectionTest.score(Integer.valueOf(100));

                reflectionTest.getClass().getDeclaredMethod("score", Integer.TYPE).invoke(reflectionTest, Integer.valueOf("60"));
                reflectionTest.getClass().getDeclaredMethod("score", Integer.class).invoke(reflectionTest, Integer.valueOf("60"));
            }
        }

        運行結(jié)果:

        int grade =100
        Integer grade =100
        int grade =60
        Integer grade =60

        如果「不通過反射」,傳入Integer.valueOf(100),走的是Integer重載。但是呢,反射不是根據(jù)入?yún)㈩愋痛_定方法重載的,而是「以反射獲取方法時傳入的方法名稱和參數(shù)類型來確定」

        getClass().getDeclaredMethod("score", Integer.class)
        getClass().getDeclaredMethod("score", Integer.TYPE)

        19. mysql 時間 timestamp的坑

        有更新語句的時候,timestamp可能會自動更新為當前時間,看個demo

        CREATE TABLE `t` (
          `a` int(11) DEFAULT NULL,
          `b` timestamp  NOT NULL,
          `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8

        我們可以發(fā)現(xiàn) 「c列」 是有CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,所以c列會隨著記錄更新而「更新為當前時間」。但是b列也會隨著有記錄更新為而「更新為當前時間」

        可以使用datetime代替它,需要更新為當前時間,就把now()賦值進來,或者修改mysql的這個參數(shù)explicit_defaults_for_timestamp。

        20. mysql8數(shù)據(jù)庫的時區(qū)坑

        之前我們對mysql數(shù)據(jù)庫進行升級,新版本為8.0.12。但是升級完之后,發(fā)現(xiàn)now()函數(shù),獲取到的時間比北京時間晚8小時,原來是因為mysql8默認為美國那邊的時間,需要指定下時區(qū)

        jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&
        serverTimezone=Asia/Shanghai

        參考與感謝

        [1]

        Java業(yè)務開發(fā)常見錯誤100例: https://time.geekbang.org/column/article/220230


        最后,關(guān)注公眾號Java技術(shù)棧,在后臺回復:面試,可以獲取我整理的 Java 系列面試題和答案,非常齊全。







        關(guān)注Java技術(shù)??锤喔韶?/strong>



        戳原文,獲取精選面試題!
        瀏覽 78
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

          <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久久久久 豆花视频 | 三级黄色性片 | 免费无码视频一区 | 丰满老妇高潮一级A片 | 深夜福利国产精品 | 91aiaiai | 亚洲爆乳无码一区二区三区APP | 天天撸视频 | 午夜主播福利 | 性爱视频福利 |