1 00:00:00,000 --> 00:00:04,896 [MUSIC] 2 00:00:04,896 --> 00:00:08,820 Now, I realize that this might cause some trauma. 3 00:00:08,820 --> 00:00:10,500 But work with me here. 4 00:00:10,500 --> 00:00:15,650 So imagine, if you will, that this code here to connect to our legacy database 5 00:00:15,650 --> 00:00:17,490 is repeated hundreds of times. 6 00:00:17,490 --> 00:00:20,000 Every single time we need to connect the database 7 00:00:20,000 --> 00:00:23,830 this same code dance happens over and over. 8 00:00:23,830 --> 00:00:25,400 And it's a bit of a hassle. 9 00:00:25,400 --> 00:00:26,480 So let's take a look at it really quick. 10 00:00:26,480 --> 00:00:31,405 So we get a database and then we open up a try block because this connection 11 00:00:31,405 --> 00:00:34,475 could definitely throw an IO exception, and 12 00:00:34,475 --> 00:00:38,939 sometimes this connection for some reason it comes back closed. 13 00:00:38,939 --> 00:00:42,976 So what we've done as a team is we've made sure that everybody knows that, 14 00:00:42,976 --> 00:00:46,132 hey make sure that the connection is open before you do it. 15 00:00:46,132 --> 00:00:48,897 So it's become part of our dance, it's become part of our ritual, 16 00:00:48,897 --> 00:00:50,300 we need to do this. 17 00:00:50,300 --> 00:00:54,765 So we open it up then we go ahead and we execute and we get some stuff back. 18 00:00:54,765 --> 00:00:57,665 This is kind of really the meat of what the whole thing is trying to do, but 19 00:00:57,665 --> 00:00:59,905 look at all this code surrounding it. 20 00:00:59,905 --> 00:01:01,825 So we're going to catch that exception, right, 21 00:01:01,825 --> 00:01:05,005 because maybe this connection opens up and it doesn't work or whatever. 22 00:01:05,005 --> 00:01:07,305 So we want to catch that, we need to catch it because it throws that. 23 00:01:07,305 --> 00:01:10,775 And then finally we need to make sure that we clean up, right? 24 00:01:10,775 --> 00:01:13,215 We don't want to leave these things dangling out there but 25 00:01:13,215 --> 00:01:14,425 there's a problem there too, right? 26 00:01:14,425 --> 00:01:17,075 So the cleanUp also might throw an IO exception. 27 00:01:17,075 --> 00:01:21,920 Now this is a pretty intense stance and because it's so 28 00:01:21,920 --> 00:01:24,540 intense and there's all these places where it might happen, we've copied and 29 00:01:24,540 --> 00:01:26,370 pasted this all over our code base. 30 00:01:26,370 --> 00:01:29,000 It's nobody's fault really because they were told this is the dance 31 00:01:29,000 --> 00:01:30,270 that we need to do. 32 00:01:30,270 --> 00:01:32,710 So let's go ahead and let's run it and let's just see what happens. 33 00:01:32,710 --> 00:01:36,350 Now I have gone ahead and I mocked all the stuff out. 34 00:01:36,350 --> 00:01:39,290 So basically, I let us know when things are happening. 35 00:01:39,290 --> 00:01:42,900 So we know the new database was called, the connection was opened. 36 00:01:42,900 --> 00:01:46,230 Then we run our query and then we call cleanUp which closes the connection. 37 00:01:46,230 --> 00:01:49,580 So that's what this cleanUp method here does is it kind of closes the connection 38 00:01:49,580 --> 00:01:51,270 that, any connection that was open. 39 00:01:51,270 --> 00:01:55,347 So let's take a look really quick at our legacy database. 40 00:01:57,489 --> 00:01:59,827 That's interesting, it implements AutoCloseable. 41 00:01:59,827 --> 00:02:03,145 Now this is part of, Java 7 did this really cool feature so 42 00:02:03,145 --> 00:02:07,940 that you could kind of help resources that need to be closed afterwards can do this. 43 00:02:07,940 --> 00:02:12,940 Now I notice that ours is not using this try with resources block, 44 00:02:12,940 --> 00:02:18,550 it's still doing this this finally move here so let's go ahead and let's fix this. 45 00:02:18,550 --> 00:02:22,030 Now what I want to point out here is that we evolved 46 00:02:22,030 --> 00:02:26,380 our legacy database here to meet the Java 7 standards, but people aren't using it, 47 00:02:26,380 --> 00:02:29,830 people are copying and pasting and that's because it's still a bit of a dance. 48 00:02:29,830 --> 00:02:34,305 Let's go ahead and I'm gonna make this, so the try with resources block, 49 00:02:34,305 --> 00:02:35,810 you open it up like this. 50 00:02:35,810 --> 00:02:40,570 And then go ahead and put that in there and we're going to lose our finally here. 51 00:02:42,040 --> 00:02:47,955 I'm gonna go ahead and, we need to catch an exception now though, right? 52 00:02:47,955 --> 00:02:52,040 Cuz the autocloseable will throw an exception so. 53 00:02:53,640 --> 00:02:56,530 This is kind of what we look like here. 54 00:02:56,530 --> 00:02:59,590 Actually, guess we can probably lose this catch, right? 55 00:02:59,590 --> 00:03:03,170 And we'll just catch any sort of exception that comes through. 56 00:03:03,170 --> 00:03:05,880 All right so, what happens is anything that's inside of this, 57 00:03:05,880 --> 00:03:08,453 this try with resources, now it will automatically close. 58 00:03:08,453 --> 00:03:10,560 Now this is still a dance, right? 59 00:03:10,560 --> 00:03:13,770 We still need to, we're still making sure this connection is open. 60 00:03:13,770 --> 00:03:15,490 We're still going through here. 61 00:03:15,490 --> 00:03:20,270 We could say, hey everybody on staff, look, this is how you do it now. 62 00:03:20,270 --> 00:03:23,756 Don't worry about that finally block anymore now that we have this try with 63 00:03:23,756 --> 00:03:24,376 resources. 64 00:03:24,376 --> 00:03:28,320 But I really think there's a lot of code that's duplicated here. 65 00:03:28,320 --> 00:03:31,490 Now, not only is the code duplicated and that's a problem, 66 00:03:31,490 --> 00:03:34,874 that you're typing these things over and over again and you're doing the copy and 67 00:03:34,874 --> 00:03:37,200 pasta, basically you're just copying and pasting this, right? 68 00:03:37,200 --> 00:03:39,584 Let's be honest, you're copying and pasting this block of code and 69 00:03:39,584 --> 00:03:41,980 people are copying and pasting the older block of code. 70 00:03:41,980 --> 00:03:45,740 So it's hard to shift everybody's mind to the new way of doing things. 71 00:03:45,740 --> 00:03:49,450 This is pretty much better, it's not the best, right? 72 00:03:50,490 --> 00:03:53,740 One of the things that's really horrible is that innovation has stalled right? 73 00:03:53,740 --> 00:03:56,445 Like let's say that we wanted to do connection pools, right? 74 00:03:56,445 --> 00:03:59,910 It'd be kind of hard to do that in each one of these patterns, 75 00:03:59,910 --> 00:04:03,350 right, you'd have to change the way that this getConnection comes out here. 76 00:04:03,350 --> 00:04:05,150 What if we wanted to do transactions, right, 77 00:04:05,150 --> 00:04:09,195 like let's make sure that you only want to have this happen if it was successful. 78 00:04:09,195 --> 00:04:12,240 That'd be really hard to do because of this dance, 79 00:04:12,240 --> 00:04:14,000 is there a better way to do this? 80 00:04:14,000 --> 00:04:17,060 There is, let's take a look at consumers.