How @Transactional works ? Transactional propagation explained ! Transaction Management #springboot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay i'll tell you to imagine a scenario right now you imagine there is a method a and there is a method b imagine method b has a block of code and method a has a block of code just like this that i'm going to show you on the screen right now imagine you got a method a which has a block of code and a method b which has a block of code right now there is a class maybe a client class which has a main method right and then this a the method a that you are seeing over here this is getting called from the main method so the main is calling the a and the a is basically calling the method b this is like our simple code base that we have where we have a main method which invokes the method a and the method a is basically having a block of code then it calls the method b and then the method b code will be executed and once it is done then the code of method a will be um you know invoked and then the program will come over here and it will go back to here and it will be terminated right so a simple setup code imagine we have so everything working good but one day your client came and said hey i need the transaction management implemented in my code base so as a day if you learned spring framework you go ahead and you maintained a transactional annotation on top of your method a and on top of your method b and the things are resolved because you know that right now we will have a transaction created in method a and then all this code of method a then the code of method b then the code of method a will be there all under one transaction just like this you you have some block up code of method a then the code of method b that will be invoked and then the code of method a this will be invoked and you will be wrapping that off with a begin transaction and a commit transaction but before you begin the transaction you know that you need to be connected to the database so the transactional annotation will make sure that it will acquire a connection uh from your database server because you need to connect to the database and then uh you know you are beginning the transaction right over here then all your db code or whatever the code that you have written that will be invoked and at last you'll be committing the transaction and in case there will be any exception occur here in this code you will be rolling it back okay and at last you'll be closing the connection and all this is right now possible using the transactional annotation right now all right so all these things are right now possible because our method has a transactional annotation if you're going to see it a little more in-depth what works it does so basically imagine whenever this method a will be called from the main method the method a is saying that hey i got a block of code here which need to be there under a transaction so method a code will be needing an active transaction in order to invoke this code so a transactional will ask okay do i have an active connection already there in my application and in this case of course no we don't have any active transaction yet by the time the method a invoked because method a has been called from main method and main method does not have any transactional annotation or does not have any code which will create a transaction for you so that method a will know that okay i don't have any active transaction for my application so far so the add transactional annotation will do what it will acquire a connection from your database server and then it will begin or start a transaction or it will create a transaction for you and then this is where your transaction will be created right over here before any of your method a code will be invoked and at last right here your transaction will be committed or it will be rolled back in case there is any exception occurs in between and then it will wrap off the connection or close the connection so right now you can see in this method a method b is getting called so there is a call is going from from the method a to the method b okay so right now method b also got a transactional annotation so method b again asked okay so i got a bunch of code which has to be under an active transaction but right now before it creates a new transaction right here it will ask okay do i have an active transaction already created and now the method b will see that okay who called me okay now the method b has been called from the method a so dodge method a has an active transaction already well the method a has already has an active transaction because it did create a transaction right here and method a already started a transaction so what method b will do it will it will not create a new transaction or it will not start a new transaction rather it will reuse the transaction which has been created by method a very important point method b will not create a new transaction it will it will use the transaction which has been created by the method a so method b will acquire the same transaction which has been created by method a and executes this block up code and once the core ends over here the flow will go back to here then this couple of lines will be executed and at last the commit will happen over here and if anything goes wrong then the rollback will happen and then the connection will be closed okay so it will it will it will be like this so method a code basically executes then the b codes basically executes and then again the a course will be executed if there is any after the b code and it lasts it will be wrapped under a same transaction okay and this is what the eight transactional default behavior is and even though you know it or you don't know it this is what is happening behind the scene but hold on i got a question for you so this is what we have seen so far this is how our code flow is looks like okay a got a transactional annotation so it is basically creating a transaction right over here is there is no transaction has been created by the method which is calling a obviously method a has been called by the main method and main method does not have any transaction created so the a method has a transactional annotation so a method has to run all this code under a single transaction so a method sees that there is no active transaction is there for its application so it is created one transaction and make sure that all the score that it has that will fall under one transaction but my question is that here a is calling the method b okay and now b is basically reusing the transaction which has been created by method a so basically the thing is that the b is reusing the transaction which has been created by the method a and this is what we mean by the transactional propagation the transactional propagation is basically reusing or how you are reusing the active transaction in this case a is propagating the transaction from a to b so the method call is going from a to b right and the b is reusing the same transaction which has been created by the method a okay so when the transaction is getting propagated from a to b b is reusing the same transaction but that will not be the case every time isn't it so now let's say your client says hey what i need is whenever the method b is getting called from the method a do not reuse the transaction do not reuse the same transaction which has been created by the method a rather you wrap this off under a different transaction so what your client wants right now all the code that you have inside method a will be run under a separate transaction and when the call will go to method b the b code will run under a different transaction okay or maybe your client will say tomorrow hey i do not want any kind of transaction to be done for the method b i want the method b code should run under no transaction there is no transaction required for method b the b code should run non-transactionally where the method a code should run transactional you can have a lot of variety of scenarios when you know you want to maintain different type of transaction level transaction propagation label so it is very obvious that you can have different kinds of scenarios like your method b need to be there under a separate transaction or the method b should not have any transaction at all it should not even use the transaction which has been created by the method a you can have different types of scenario that can be resolved by different types of propagation level which has been given by the spring framework so you have different types of propagation level like required required new mandatory supported not supported nested maybe we have five or six different types of propagation label and this is what we're going to be exploring today by writing a lots and lots of code by writing some code in aop by writing some code in dynamic proxy by writing some code in spring framework and what more you can expect from me right now i'm pretty much excited because i feel this is going to make you a master of this topic ah it's a big statement to make but yeah it is going to make you make you understand the concept a little more easier way and i hope you're going to be finding it interesting and in case you do find it interesting let me know in the comment section and in case you don't find it interesting also let me know in the comment section so that i can delete that comment just joking let's understand this topic in a step by step manner and i'll see you in the video [Music] [Music] all right okay so basically as i said we'll be discussing about the transaction propagation so the transaction propagation means how your active transaction that you have how those transactions will be reused okay that's what you mean by transaction propagation level okay so coming back to the transaction propagations we'll start seeing like you know different propagations that we have in our a transactional annotation how we will be using that and it will be very very simple to understand um but my main intention of this lesson will be like you know how can i showcase those things to you so that you guys can do it by yourself you don't have to remember anything um maybe i'll maybe i'll just do something do some setup today just to show you like you know how you can see like you know when the transaction is getting created when the transaction is getting committed when it is getting closed these things i will showcase to you so you can just apply the same tricks and you don't have to remember anything anytime you can just use those tricks and can see like you know um everything in detail uh you know it will be very simple to understand the thing okay uh going forward i'm gonna do one thing i'm gonna be copying uh this uh spring project i'll be creating a new spring project because i will push the number one code to your you know repository in your lesson material you'll be getting this one okay so right over here and also i'm thinking to give this lesson in youtube so if you are if you're watching this video in youtube or maybe if you're watching my transactional video in future in youtube or any other free platform in my website or somewhere else um then maybe you can you can find the code in the github repository and i will be um placing the code uh link in the you know in your video description you can check that out right there okay coming back to our project that we have created yesterday i remember we did have a basic setup right now and i think a pretty much straightforward thing that we have done in our configuration file uh basically we did set up our data source we did set up our jdbc template and we have set up a transaction manager because we want to create a transaction manager and this data search transaction manager will maintain like you know how the transaction will be created committed roll back and everything and in order to active the transaction i have told you guys to use that at enable transaction management yesterday and uh right here inside the repo i have created a product repo where i have created a method called seb product where i have written few logic like you know how i have created a sequel query for insert statement and i have passed in some arguments updated i'll arrange update method to execute the sql sql statement using these arguments that i have captured from this product uh you know arguments right here and this is something a transactional method and i'm calling this method from a service class that is going to be here okay and this is my safe product info method where i'm calling the say product method for 10 times all right in order to save the product like no 10 products will be saved when this for loop will run and if any exception will be thrown it will be rolled back so right now for now i will be removing uh maybe i'll just comment out this um you know um the exception uh stuff over here so i just want my 10 products to be uh to be pushed to the database smoothly and i'll be removing the propagation stuff right here because we'll be starting it from scratch today so maybe i'll remove this code okay and there you go i think things are good right now perfect so this is what we have done we have created the service method we are calling the repository method right here we are creating 10 products here when the follow fronts one one product will be created and one one product will be pushed to the database and this service method will be called from the client that we have inside the app.java so here we are just creating our application context if you remember and we are loading the context uh we are creating the context out of the config file that we have written here and if we're going to go back to the app.java we are registering the shutdown hook uh to close the context and because the standalone application and we need to register the shutdown hook and we are just getting the service been over here and we are calling the share product info method and this is the method which is annotated with the transactional annotation so as i have told you guys that here spring will create a transaction right here and all this method code that you have including this this transaction will start here and then you know what this 10 products will be like you know the for loop will run one product will be created the save product method will be called here we got a transactional annotation and by default this transactional annotation is going to use the same transaction created by this safe product method and then it is going to you know push the statement or push the install statement it will actually it will prepare the statement it will insert the data to the database and it will be done for 10 times and once it is done here only the commit will happen the transaction that that is getting created right over here the transactional annotation will create the connection it will start the transaction and also it will commit the transaction and it if something is happening it's gonna roll it back right one important thing you need to understand here is that i wanted to tell something but i forgot because of the chat okay let me see here good morning everyone thank you prashant good morning yeah tell me okay so i wanted to check that uh do we need to use transaction on both the methods in the repository and the service destiny is not enough yeah so basically yeah basically if you are not using any transaction annotation in the repository you can just comment this out as i have told you guys that the transaction will be started here why it will be started here because by default what is the propagation label okay the propagation label will be here required by default this is the default propagation label and what do you mean by record required means requirements here if you're going to go over here you can see this one say this light here present present here you can see request means support a current transaction create one if not exist so if the transaction is getting created any other any other method is annotated with a transactional annotation if if uh the the current transaction if it is calling the another one i'll be telling you for an example here here i got the set product info so the transaction will be stacked here why because i have a transactional annotation so this by default the propagation label is required so the transaction will start here and by the time the shared product will be called here also we got a transactional annotation let's say okay and if here by default the propagation label will be what by default it will be required so what do you mean by required it will use the transaction if it is already created right now who is calling this method the subproduct method is getting called by the product service right the products are the say product info is calling the self product method now the transaction is created over here so this method will check if there is a transaction exist if it is exist then it will use the same transaction okay and then this method only will commit the thing right here okay what about you don't have any transactional annotation in the service class that means this code will be executed non-transactionally but whenever this piece of code will execute and when the flow will go to the step product this transaction annotation will check whether there is a transaction has already been created no there is no not any transaction has been created yet why because our code flows start from here after java here we are calling we are getting the product service class object and we are doing uh like you know product service class subject from the context get been and here we are doing product service dot seb product info now here no there is no transaction here also there is no transaction because this is commented out and whenever the shareproduct method will be called here it is going to check do we have a transaction annotation uh do we have a transaction already created well there is no transaction yet so it is required so i have to create a transaction i have to make sure that this piece of code need to be executed within a active transaction so the required will create one transaction if it is not exist if it exists that means if this method has been called by another method which got a transactional annotation then it will not create any new transaction but here in this case a new transaction will be created and this code will be committed under a particular transaction okay now okay is it making sense yeah but just one thing what i remember from our last sessions in the past that we only have to annotate that transaction in the service layer exactly exactly exactly because you know okay it's not mandatory that's the common use case because you'll be you'll be seeing it by the end of this video okay why i'm saying it's a common use case because we can override the propagation label over here but if it is not required now in the prop uh the service this is the service i will be writing the transactional annotation over here so right now the transaction will start here then this piece of code will fall under this transaction this will call the scepterdoc method the say product method whatever the code that we have it is also going to fall under this transaction only because this is already marked with a transactional isn't it right correct okay now this thing that i'm saying that required it will create a new transaction here uh how can you trust me with that i need to show you the practical proof of it a proof of this right so that thing i'm gonna be doing it today if you're gonna see there are a lot of different propagation label it says as i want to repeat it propagation something means that how your transaction will be reused let's say you are creating a transaction here let's say you are creating a transaction here okay do you want to reuse the transaction no i don't want to reuse the transaction right here when the method call will be called when this method will be called i want to maintain all these things as part of a different transaction so maybe i'll go with a uh another you know propagation label called let's say request new request new means from here it will suspend the previous transaction if there is any transaction exist and it will execute all this code as part of a new transaction and it will commit it over here okay so how are you gonna be basically mastering these things how you're gonna be you know avoiding the confusion you don't have to listen to me the word that i am saying right now which might sound confusing for you if you're gonna new to transaction so i'm gonna give you some trick okay and if you remember the dynamic proxy concept in the last session maybe we'll be using it a little bit um and i'll be giving you some tricks so that you can just do some handstand in the next uh maybe in this weekend and maybe we can carry forward from the next class guys i'm taking i'm thinking to take a extended session uh maybe for um maybe one and a half hour today because i'm not feeling comfortable i i want to cancel the class for tomorrow i want to sleep uh to be very honest and um you know i want to go for extended session even though i'm having a headache so you guys just keep me and guess uh because i think if i'm gonna take the if i'm gonna take your session i'll be i'll be feeling good because i was with a client called for the last couple of hours and like you know chopper shopper for unnecessary things and you know i don't know i certainly caught a headache and it's actually happening a lot for me you know for the last couple of weeks so yeah so anyhow uh so i will be giving you so many tricks today and you're gonna be applying those tricks you will be doing that practical by yourself and you will be telling me that if the things are making sense in the next class and i will be taking it further so in the next next week also for the entire week we'll be talking about the transactional stuff okay now coming back to the class now i will do one thing okay i have now i'll be deleting all the you know stuff that i have over here and i'll go to here i'll just remove all the propagation levels that i have over here now things are looking good okay i want to ask you a question okay the question is for an example guys i want to log a couple of things like when this method starts okay when the subproduct method start i want to uh log something like sys out okay uh method started let's say i can say um let's say product info i want to do something like this okay method started say product info this method has just been started in when the method end i'm gonna log something out like this okay because i want to track few things so method completed say product info now if i'll be running this obviously you know it is going to tell you that um like you know nothing fancy it is not telling you duplicate entry one because we already have the data right there for an example can i run this and ctrl a okay apply apply close okay can i go back to here if i'm gonna run this again okay i'll be getting this locks for sure right so haven't i saved it started products there products say why this unlock did not print it here runtime exception occurred or maybe previous application i'm running or something can i just go over here i have already changed the code right transactional 2 i'm running which one i ran a4 okay let me just check let me go to the database execute the code there is nothing here okay can i go back to here can i go to the class to a right click run as java application let me run it normally okay now it's just giving me the right result i'm logging something right here whenever the share product info method called i'm logging when the method is starting when the method is ending i want to do it for all the service method let's say i have many service method right now annotated with a transactional or something else i'm doing it and for all the service method when i'm executing any service method or whenever i'm executing any down method here also i want to print uh started the select product method and completed the sep product method and i want to do it for all dow method and for all service method so it will be very difficult to write all these lines inside each of my service method isn't it and inside each of my dow method so what is the solution out of it anybody remember anything like if i want to find a solution then what will be the solution [Music] spring aop correct spring eop so right now basically it's pretty simple right we um now we can think like um here we have a um we can think like we have a service service object right and then inside the service subject if you're gonna keep logging the statements the things will be uh you know messy like you know the code will be bulky so you'll be moving these things to some class let's say uh logger or maybe i can say call tracker okay and this is going to be an aspect why i'm calling you a aspect because all the non-functional code this is not business logic this is a simple logging this non-functional code previously here previously this non-functional code was here like some csout was here okay and this uh this logging statement was inside the service code only okay now i'm gonna be moving this code to to this call tracker and this is what i am calling an aspect so what is an aspect you just ask yourself a question which are the non-functional code obviously those loggers are the non-functional code and i'll be moving it to where i'll be moving up i will be moving which code i need to move okay which code which which score are non-functional and which code i have to move to a separate class and i'll be moving this non-functional code to a separate class called a call tracker and this is going to be my aspect in aspect-oriented programming okay uh aop there are some term aspect uh join points uh advice okay what do you mean by that so maybe don't get confused if you're hearing for them for the first time just follow me over here if you have completed my eop session then that is good if you have if you are not then no worries just remember that all the non-functional code like security transaction logging tracking tracing all will be maintained inside a separate class and as they are the non-functional code i will call them as aspect okay so maybe i'm gonna there is some exception there is some error okay ctrl a ctrl shift f and i'll be removing this thing and what i'll be doing right now i why i'm i'm getting a problem okay i need to end it over here then what is the problem okay now things are good right now i will create a new package so i'll say new class and this is let's say this is going to be a call tracker okay and i'm gonna be putting it inside a package called let's say dot aop okay do it finish and here inside the call tracker class what i so this is my which class aspect class okay why i'm calling it is the aspect class because or as i have told you aspect i'm not getting the aspect annotation oh because i remember correct correct so which one was pending so we have added aop oh aspect for spending huh so i got something on the channel aspect j is aspect j annotation maybe i can go to the spring as the same one only so make but i will get the spring one so what what actually aspects yeah let's check why i'm not getting that is it us aspects or something yeah this one aspects copy this one i'll get this one so mike that one will also work because internally we are using that only so paste it ctrl a ctrl shift f control s so we got this thing right here let it build it let me go to the call tracker and aspect this annotation i will um i will be writing over here and i also want this uh class to be automatically detected by my spring and make sure this this aspect is not a stereotype annotation it will not make your class component scanned and for that you need a annotation called at the right component because i want spring to scan this class and create the object for that and obviously spring will create the proxy out of this object right um cool so basically now we got the aspect so what do you mean what do you mean by aspect just ask yourself a question what code need to be uh need to be brought here need to be what code need to be here all right which score you need to be bought here to this particular class okay pretty simple question which are your non-functional code dump it right here and i will be dumping it you'll be dumping it dropping it inside a method let's say public let's say void let's say i want to do some logging before the method has been called so before the log before method let's say log before method call okay and there there should be some annotation let's say i want this is not spring there is nothing related to transactional i'm just creating a setup which is going to make your transactional journey easy okay so you can just use a annotation called at the rate before and this is gonna help you if anything you're gonna be writing here let's say cis out let's say execute this line before any method right so this before is called a advice okay why why why you are calling it the advice just ask yourself a question this bunch of logic that you have written here it need to be it need to run when okay when this particular thing should run so when the logic here should run okay so is it run before the method is it going to run after the method call is it going to run before end after the method call so when this logic you want to run that is called a advice you are advising the spring framework that hey spring framework do one thing whenever any of my repository method will be called or whenever any of my service method will be called right make sure i'm advising you to run this method which method this method right so when this method should run this method should run before or after or before and after i am i want to run it before uh let's say before each of my um you know service caller before each of my let's say dow call i want this line to be printed execute this line before any method so let's say i have a logger like this let's say i'm i'm saying list and list i'm saying method is starting okay i'm just saying something right over here now i am advising the spring framework that hey run this particular code that i have inside here before every method right so before which method okay which method before which method this particular code will be running okay this is what we'll be defining as a point caught right i i need to run this code but when we before we before which method i will have service my i have some config classes i have some repo classes i have some service classes i have methods inside service i have methods inside repo i have methods inside this package i might have thousands and thousands of packets but which are the methods before what i want to execute this logic this is what i'll be defining as a point card okay and i'll be defining here that let's say public void let's say um log method point caught okay this is what we call a point cut okay and point in the point caught we'll just tell like at the rate point called annotation and we're gonna be telling here like okay before which method we want to run like before i want to run that particular method that i'm defining before every uh you know method that i have inside the product repo or inside my um you know repo class i have thousands and thousands of classes before every repo classes i want to run this particular method right so i can do it in various way i can use execution i can use within and let's say i'm writing within before each of my service method i want to run so this is my service class let's say copy this um you know package name control c sorry paste it here maybe and this is a very long name right i will do one thing i will just use a shorthand com.service dot star i think this will work okay so now before each of my service method i want so i have defined the point card so i'll call this method here inside the before that's it so i will call this method here there you go right your code is ready so i think i know i have just revised your aop concept a little bit if you're not understanding also if you have very new to aop don't worry whenever you'll be learning it more you'll be learning it more but i think i made it simple right just ask yourself what code need to be moved here okay these are all the best less score i don't need to put this code inside a business class right inside the business method because i'm just logging something i want this method to be executed i want my spring framework i'm advising my spring framework that hey execute this particular method before each of the service method okay and this is how i'm advising the spring framework by writing it before annotation and now let's see that whether this is working but this code will not work because you need to do one more thing inside the config class activate the spring aop and the way you will be activating it by using let's say enable aspect j auto proxy i think this is the one so this will this will help you to create the proxy um for your class so what it will do so um you know you have your service class right this is your service class and this is your non-functional code here so what will happen in runtime you can think there is a proxy will be created okay there is a proxy object there is a proxy class will be created and in that proxy class imagine there will be some method which will do what which will call this method okay it will call this method and then also it will actually make a call to your service class right you are defining here right uh execute all this this method before each of the service class methods you are writing com.service.star server.star means inside service whatever the classes that i have inside inside the service package i have thousands of class inside every class methods make sure to run this particular piece of code right so right now if you're gonna go to your service class let's say i have a service class here i have this method so what will happen this proxy will run this log before method call so this method will print some log and this method will actually make the call to the service class and whatever the logic that you have written here inside the service class this logic will be executed so instead you are making a direct call to here okay what what you are doing you are actually creating a proxy and that proxy is getting created out of your uh this particular things that you have written that aspect that you have written and what is the proxy is doing the proxy is making sure that it is calling this method and also it is making an actual call to your business code so instead of you doing it by yourself by making some bulk by writing some bulky code the proxy object is doing it for you we don't give a damn like how it is happening just telling you right in case you are interested now i think my code is ready so now let's do let's go to the after java one second i'm gonna check the chart let me just run this first let me see whether the things are working or not we got some error okay why the error is coming uh point card is not well formed okay what what the chart temp is getting a vlas please plug in login oh okay okay thank you thank you one second one second otherwise i would have disconnected right now okay thank you um so can you guys help me why my point card is not working it's saying that my point card is not well formed let me go there um public void yes we have not given the method i think for service.star for all the service classes but what about the methods i want to give i want to do it for every method because because i have not added this one within because either i have to either yeah either i i have to do execution either i have to do execution or i can do within i need to use a method okay there are different way you can form a point card right don't worry about that you just write this keyword for now within and this is my package and i'm telling like you know use this particular point called so whatever the things that i'll be defining here now each of my service method the things will i am expecting that this line need to be printed right let me go and this one no let me just run this okay method is starting but there is some yeah there is some exception right now in the prepare statement because the duplicate value maybe i could have used the auto increment of the id so that i don't have to delete this thing again and again but that's okay apply apply close go back there uh click over here five and there we go method is starting right so before my service class okay before my service score executed whenever i made a call over here inside the product service.java right here i'm sorry ah this is my service method i'm calling it inside after java right here whenever i'm calling it the aop is making sure that it is gonna execute that particular method that i have written here okay this before method it's advice this is a this is called advised right so i'm advising my spring framework that hey run this method before my service method executed you want to also make sure that this should run before each of your down method so where is your dowel code inside the repo right here this is the package name copy this maybe what i'll do i will just keep a or copy this guy one more time paste it here and this time it is repo that's a repo dot star right now this line will run before each of my service method and before each of my run method because before each of my repo method because this is what i have told in the point card and my advice which is before is right now using this point card and this point got i'm saying that hey um like you know execute this code before all the service method and repo method now let me go and see that whether that is happening but you know i have done a very dumb mistake i have not auto incremented the id so i have to delete it again and again so we'll just do that thing later we will right now just execute this one we'll see like you know what is happening and there you go look at that method is starting this is for your service method again the service method is calling if you remember the service method is right now calling the step product method which is a dow method right which is a repo method now here is also again method is saving then product saved is coming from here when the this code will be executed the product will be saved and then again method is starting why this method is starting uh because you remember inside the product service you are running a loop right and this loop is doing what it is again and again calling the sep product method so each time the set product method will be called uh the method is starting products method is starting product saved now this method is starting will be will be done for each of the safe product method call and that mean our thing is working like a charm that we have written over here and right now i want um also some kind of logger like to be there for like you know ctrl a ctrl shift f let's say log method log after method call after method call let's say method execution end or completed okay now how i'll be achieving this very simple right adapter this is also a advice why i'm calling this damn thing as an advice because i am writing a ad after annotation it after is an annotation which is a by using this annotation i'm advising my spring framework that hey spring framework make sure that in this after method after annotation whatever the point got i'm going to be defining here make sure in this point called whatever i have written that after every service method after every repo uh sorry after every service class method after every repo uh package inside every class which is present inside repo class repo package and whatever the method that i have inside the repo in our package after everything you make sure to execute this particular piece of code and i think this will work fine as well now can we go over there and let me execute this one let me just delete this and apply apply close there we go and now let me just run this one more time and you can see if my code is correct now you can look at that method is starting this is your service method now method is starting this is your uh you know dow method then one product saved then dow method completed okay your repo method completed then again the method is starting that means again there is a product service product services again calling your repo method and again the product is saved and again your repo method has completed or your dow method has been completed now your dow method is starting saving the product completing the code starting saving the product completing the code and in the end you can see two times method execution completed because one time if you remember your for loop ends there your for loop is basically ending here right so last time this will be called after that this method also will be completed so that's why two times method execution completed this method completed at last like when i will be 10 and again the method execution completed because this method is completed then the call tracker that we have created is working like a charm but right now i am going to combine this couple of things so the way i'll be doing it i'll remove this actor i'll be using an annotation called around okay the round annotation will do what it will help me to run this method before each method call and after the method call before the method call and after the method called it is a combination of before and after right before and after so let's say i will be just giving you a trick now i want to see i want to use a round annotation and i want to write this method let's say star star star star star then what will happen now before the method will start before any method will start of the service package or the repo package this um will be called and after that particular method has been completed this will be called maybe i'll be showing you maybe the way i'll be capturing which method is getting executed i can use a join point here proceeding join point proceeding join point and i will be doing what i'll be using this proceeding join point that i have written here proceeding join point dot proceed and the proceed method will do what it will help you to execute the method let's say any method is getting executed you know so let's say right now imagine you are your aop is calling this method say a product info now what this particular method is returning or or maybe i will do one thing for now i will not be writing this thing i'm just writing proceeding join point and here i'm gonna be logging something let's say this out let's say proceeding join point dot get uh signature dot get name okay so this is gonna get me the method name let's say i'm gonna i'm gonna say uh method start okay and then once the method has been start i'm gonna i'm gonna invoke the method so i'm gonna say proceeding join point can i okay proceeding join point dot proceed that proceed method will invoke the method like you know whatever method is there like let's say any method in your service class or in your you know repo repo class those method i need to invoke and that method will return something the way i'll be once again uh cancel the one the way i'll be capturing it control one assign statement and this is gonna obviously every method will have a different different return type i'm writing object here so this is going to be the return value for your you know maybe i can print it out for your understanding maybe i'll return this object also and i'll print it also let's say this out and i'll say return value and also i will make sure to return this return value so any method will be executed that return value i'm actually capturing and this is actually an object because you know this is something that i'm returning now now you'll see like you know what will happen now this is the starting point okay i'm logging something then actually i'm invoking the method now you can you are understanding what i'm trying to do what i'm trying to do is now imagine there is a method i want to call this method but you know what i want to log something before the method start i want to log something after the method after the method executed and if you want to do it you will be putting a log here and you will be putting a log here but i don't want to do that i want to separate the those code and i'm i'm going to move it to a different method and what i'll be doing it over there here so basically i am making the call to this particular method inside this guy inside the log before method call so this is actually the method call like you know any point in let's say in your service class there are ten methods right now i want to invoke this particular method called say product info so my client will right now my client was directly calling right so after java was directly calling this a product info method now what will happen i'm intercepting this particular call by by how i'm how i'm intercepting that particular call i'm intercepting that by using this aspect right i'm creating a proxy out of it and what this proxy will do so look at that here i'm actually invoking the method okay any method so one by one one by one method the method will be in work you can imagine like inside the product service i got a method called save product info so the save product method will return me now it will not be returning returning me anything so that call is right here so this is the call i'm gonna do proceeding join point here i am basically taking the join point join point will give you the method which is getting executed it will give you a execution point and i am doing a proceeding joint point dot proceed so that means let's say say a product info method is getting executed i'm invoking that particular method and if it is returning something that value i'm capturing obviously say product info if you're gonna see it is not going to return anything it is a void method right it is going to give you no so if i am printing it this value will be null here right so this will be a null value for an example and then i am returning the value just for your understanding and now the main important thing is that this is like my method call right but before my method call i'm printing something and after my method call i'm gonna print something this is going to be before and this is going to be call to your method and this is going to be after after the method executed you can do something over here let's say method completed i can say so i'm bringing the before and after together method completed and here i can just writing uh proceeding join point getsignature.getname i'm just some writing something like this right so otherwise maybe i can just do ctrl 1 to this and assign to a struct to a local variable and i can use this name here and i can also use this name here instead of writing the same thing again and again i can do it like this okay can we do one thing yeah instead of only printing the method name then we do like which controller which method yeah yeah we can do that we can do that but but let me come come to this one uh uh somic i don't want to go into the aop pretty much my intention is to come back from here and teach you a transactional right okay no problem we will do that anyhow we'll be doing it i'll i'll be just taking that example but now let me run this particular method let me go over there i'm just telling you so that if someone is not our of aop he'll not feel uncomfortable um so okay apply apply close can i go back to here and right now product service now i want to run my update java let's see what's going to happen right click run is java application now let's see the output you're going to see a lot of log here okay and look at that everything should make sense to you first of all the method start which method save product info what is this app product info this is my service method look at that this is my service this is my safe product info now this method is starting right how it is starting because when this method is getting called and we have a tracker here this is going to be a proxy this is going to be the middleman between your client who is making a call who is making a call here okay inside the man method you are making a call and this is going to be my uh you know proxy and this proxy is doing what it will say well you want to call this method right so you can proceeding join point means this is your method which is getting executed which method you are executing this method right say product info so this is going to be this again for for an example right now this method is getting executed proceed means execute this particular method and whatever return type it is giving this capture it but before that i'm logging the method name and that's why it is saying save product info and then what is happening we are proceeding the method we are printing the value obviously that method will um you know what that method will not return anything right so obviously right now uh you can see okay okay one second say product info and then the save product info method is getting invoked over here what is the value it is returning this is the next this is the other thing but right now when this method will be executed internally what will happen so say product info will call the another dow method our repo method called say product right then again for this method also i'm printing the thing why the print is happening because you can see this uh point caught okay this point that i have defined i have also written that okay whatever the logic that i have written here also that will be applied to the repo method as well as the service methods so that's why here method start save product save product is what say product is your right now where you are making the call product service so here you are making product report.save product this is your service method this is your service call and inside your service you are making a dow a dial call and it is logging that method then product is getting saved it is not returning anything okay so it is saying null then again it is happening method completed right now method is getting completed why because you have written this line right method completed here this is your start this is your you know point of you are executing invoking that method and completing that method and that thing is getting started if this is your service method stat this is your dow method start product has been saved and obviously it is not returning anything your dow method is not returning anything if you remember um the dow here product service is making a call to here and the dow is returning void that's why it is printing null because you remember in your call tracker you are also doing system.out.println and you are printing the return value here once the method invocation is completed here only the dow method is right now getting invoked and now the dow method is getting called many times okay save product again got called is your dow method and again it is got saved and again it is not returning anything again this dow method start because the loop is executing right the diameter of the ripple method is starting saving the product okay um repo method is completing then starting again saving the product then completed then starting again now i can just remove this uh return value this will make more sense this knowledge right now i don't need to print the return value i think you guys can understand let me just have a doubt on this null so this null is worth product info product uh for the report yeah this null will be for repo first okay then at last there will be another null for service because the service will return at last right okay okay locally one second one second let me run this okay i got something on my screen let me run this one second okay let me execute first you see this and now this is your service method start right now this is your dow method right say product is your dow method if you remember remember the method name guys because similar uh your service method start here say product info and save product is your repo method now look at that okay save product info service repo started saving the product repo end okay repo started saving the product repo end report started saving the product repo end the loop is continuing right and this air product log is coming from where from here from the repo you can say product repo from your product repository we have a product save call over here so this is executing again and again and at last the product info is completed now product info started here this is your service started here yes end here and this is where the product service is making 10 different calls because of the for loop and every time this particular method is executing or we're entering to this particular method i'm logging this value method start product set products product save completed start save completed start save completed this will happen for 10 times and at last your service will be completed and that null that you are saying which method is invoking okay you can think no simple now service method called here now it is it is going to invoke this method again and again and again and this is going to give you null and at last this product this will be completed and what this one will return this one will also return now so you can at last you will see another null here if you're going to keep printing that particular value magnificency null between method completed safe product and then null and then method completed correct let's do it no let's do it no let me go it go to call tracker let me just um do a control g control s do i have the products already let me remove it once you understand this i'll come back to the actual topic so apply apply guys i will not be taking any session tomorrow i'll go for extended session so if anyone got office you guys can drop at 9 or 8 30 8 45 but if you are if you don't have any office it's friday um if you are doing some chill pill kind of thing you can it can be over here okay so right now let me just to run this one one more time and you can see there is a null a kill making sense yes yes all right so right now things are good okay you guys understand this so i'm able to track my calls okay pretty fine all right so it's time to take a break you are learning from the last one hour did you realize that if you realized that then of course i made you feel bored if you didn't realize that then yeah i can take that compliment from you so this is the time to take a break so you go home grab some coffee and sit down and watch the next part of the video but make sure that in the next part we need some more more knowledge on uh you know dynamic proxy so if you don't have any knowledge and dynamic proxy you can watch my free session which is streaming on youtube so i'll have the link posted on the dynamic proxy and if you if you are a bit of experienced developer you already know what is a dynamic proxy or if you can understand the code i think you can you can give it a shot um you know you can continue along like you know watching the video otherwise you can go back and revise the dynamic proxy concept and then watch the rest part of the video and you're gonna be enjoying it so much because right now we're gonna be writing our actual code which is gonna let us know okay this is when the connection is getting created this is when the transaction is getting begin and this is when the transaction is getting committed all these things coming up after this break [Music] [Music] pretty fine now let's come to the interesting part right you guys know about dynamic proxy right we have spoke about it i'll be using that one to make you understand what is actually happening when we are making some transaction related stuff for an example here in your product uh in your product service let me come back to here now in your product service guys we have a safe product information perfect okay i'm writing a transactional okay so if i'm writing a transactional will be creating the connection how the connection will be created here guys any any idea who will be creating that connection in your spring yeah go ahead yeah transaction manager um okay so uh prashanth one thing is that that is correct the transaction manager only will create the transaction if we'll comment this transaction then we will create the connection i'll come back to this in detail inside the save product you got someone right yes okay this guy will create but if we have the transactional then we'll create the transaction manager how can you trust me i can be i can be also wrong right i'll be showing you those things right now just imagine what i have told you before this method will be called the transactional annotation will do what it will create a transaction for you it will create the connection for you right it will open the connection here okay connection will start here okay and the transaction will start here okay transaction will be committed here the connection will be closed here how can you trust me on that okay you can trust me if i'll be proving it right so guys by default it will be propagation label will be required required means it will it will start the new connection if there is uh there is no transaction exist it will create a transaction right here okay if there is a transaction exist it will not create any transaction obviously there is no transaction exist by the point of time this method call will happen because the client that we are calling till here there is no transaction created so by the time this method will be invoked a new transaction will be created because we have annotated this class with a transactional annotation perfect now here is the thing so actually i want to see that guys which okay who will create the connection for us transactional manager what is that particular connection object will be anyone like what do you feel like you know what is that connection object okay can you tell me the you know connection interface this one right what is that connection this object will be created right java.sql this is the connection and here you can see you have many methods like control o you have commit method you have get connection method we have get connection i think the get connection okay we have auto commit method we have i think get connection will be given by your the driver driver manager right yeah okay maybe who is the implementation for this guy connection will give the implementation this is given by whom this is given by oracle isn't it java x dot java.sql so right now we'll give the implementation for this ctrl t exactly exactly what is that implementation implementation class um cinebus if you remember this is simple no connection means you just look for something called connection input little bit see connection input and look at it from where it is coming it is coming from com.minuskiller cj or jvc so your mind is still giving you this detail right and this is basically implement the jbc connection and blah blah blah and right now do a control or um commit you should also have rollback you should also have closed connection all these methods are here right so i'm expecting that whenever my spring will acquire a connection it will basically get the objective connection input that means obviously it will look for the connection class object and we have given the mysql implementation because in our if you remember if i'll collapse the project which project i was working with can i do a link to editor if i'll go to the pom.xml i have given the mysql connector java so spring will basically get the my connection impul as the connection uh provider okay fine now i want to see when the transaction is happening right here inside the service where is that service class look for that service when this transaction will happen it will acquire the connection right it will do a get connection call for sure then only it will create a connection then only it will start the transaction obviously it will call my commit method it will call my close method i want to see when that method is methods are getting called i want to track those information and if you want to track it we'll do one thing we'll go to this aop we'll create another aspect okay we will just do some some other logging stuff here so i'll just say this one is a data source aspect okay and inside this data source aspect i'm gonna make sure to have a aspect annotation i want to make sure that this is a component so the you know class will be detected by spring itself i want to have a method called public void let's say i want to return it let's say public object and let's say log data source um connection info like when the connection is getting created when the connection is getting closed when the things are getting committed blah blah blah i'm gonna return all for now and i want to do it before and after before and after so i'll be writing around i want to execute this method so if before i'm doing because if the connection is getting created i'll be getting information after i'm doing because if the connection is getting closed i'm gonna get the information about it right so i have a beautiful thing here i don't want to uh you know execute this method for every method that i have i'm gonna give a target for this i'm going to set a target and my target will be okay and my target will be let's say my target will be control shift t what is that connection just tell me when this connection object is getting created maybe i'll not go for the connection let me go for the data source data source because the data source will do it internally right uh java x dot sql this this got the get connection method right and get connection will give the connection object itself so instead of um making the target for the connection we'll make the target for data source let me do right click copy qualify name and put it here inside your data source um here inside your target so i want to track everything about this data source object when it is getting created tell me everything about it right so i will have the proceeding join point proceeding proceeding join point proceeding let's say join point here so why i'm capturing because let's say any method is getting executed here on top of the data source i want to have a track of that like the connection object the let's say get connection method is getting invoked on the data source object then i want to track what kind of object it is returning i want to track that method call so i'm doing proceeding join point dot and i want to get the method right so if i want to get the method then i can just go for it you know get method let's say get name the proceeding joint nature okay get signature can you we can go for only signature let's say and let me just print it out let's say this out data source so i said i can say data source tracker or let's say data source tracker okay and i can just concatenate with this guy so i just want to see like you know i'm targeting the data source subject and i want to see i want to you know track those information right uh cool and now here i'll be invoking my join like you know whatever the method let's say get connection will be called i want to just do proceeding join point dot proceed and i want to have the return type and i want to throw the throw the you know exception it is throwing i'm gonna do assign statement to a new local variable and obviously this object let me just return directly over here okay so now let's see that whether this guy is getting um you know i'm getting my get connection object um you know over here or not so now i want to do one thing i want to uh let's say go to my mysql workbench run this and do control one sorry control a remove all the data which is present in your database now do a close go back to sds and run your application let's see right now before my transaction is getting started whether it is now look at that look at that okay so it is tracking the data source like the data source tracker that i have created over here it is just showing me that when the save product info method start right when the save product info inside my product service when this method will be start this this is my service method when the client is calling the services i have said the transactional annotation will create a new connection because by default the propagation label is what by default the propagation label is what here anyone remember is required requirements yeah requirements what it will create a new connection and look at that it is looking for a new connection here and look here do we have any other tracker for the get connection we don't have yet so we are seeing that there is no other connection is getting created okay can i do one thing can i go to this method and can i go for here the propagation label is requires new so inside my repo i'm changing the transaction propagation label to require new so let me go to my database first let me execute this and clear out all this thing apply apply close can i come back to here do a f5 run do okay let's see and look at that what happened every time yourself product method is getting called this is your what this is your dow method you can say saving the product before every method it is looking for a connection it is looking for a connection it is looking for a connection why because right now your service is required propagation label so before this method start there was no active transaction was there so it was creating a new connection and it's starting a new transaction but right now inside this method the say product method is here the say product method is inserting the data to the database and this is having a transactional annotation and the propagation level that i have set here is requires new request new do will do what it will suspend if any transaction is already exist and it is going to create a new transaction and look at that before every time the share product method will be called before every call the get connection method is getting called again you can see every time the step product method is getting called which is your repo method as it is looking for a new transaction every time it is creating a new uh connection over here now you can ask me that okay last okay i understand this but when the commit will happen now let me tell you those things now let me remove this thing okay let me go to the product service and let me remove okay we don't have anything over here okay now i will do one thing i would i'll just try to show you like you know when the commit will happen right okay so if we'll go for the a transactional annotation here i'll go to the safe product method guys you know what i'm my headache is gone it is internally there but i'm loving it right now okay now because i am also doing it after a long time so let's see that whether we'll be able to prove the thing so we'll go for the sep product method right now uh okay okay before we go for the safe product method we'll go to our data source aspect okay here we are logging like the when the method is um we are tracking the method information we will do one more thing this object is which object guys can can anyone guess like you know what what this particular thing will return you any any idea looking into this any idea what it will return you so what is the data exactly exactly why why is saying connection object anyone because inside the data source which method is there get connection method right yeah exactly and here i am calling that get connection method only right so the get connection method will be called and it will give you a connection object obviously the connection is a interface right present the connection is the interface so what kind of implementation you are expecting the connection impulse mysql exactly exactly so can i come over here do a system just want to show you that it is just returning what is this proceed this is return value let me just write return value copy this guy and paste it here paste it here uh right here ctrl s go back to my mysql workbench go here delete everything guys whenever you are practicing you make the id is auto incremented right so that it will not give you duplicate id let me run this okay let me see if it right now it is required obviously it is required right now inside the product service so the connection created over here and you can see it's a connection info coming from com.mysql.css till now guys tell me if you have any doubt or question so far i will be telling you right now how now we will see when it is committing when it is rolling back when it is closing the connection and all these things so far is this thing that i have written inside this data source aspect right here is it making sense like what method is getting executed what it is returning or any questions you have on this yeah for every transaction it will create a new new connection yeah for every transaction it will create a new connection if the transaction propagation is requires new okay okay but but here the propagation is what percentage is required required means only one trunk correct if it is already there it will be reused if it is not there then it will create one okay okay perfect perfect so now let's go back to yeah question yeah yeah i have no question yeah [Music] aspect class or the correction that you're targeting your data source class we are monitoring every method called english in this package right correct like is the instead of using that could we also use point cut especially you can use point cut but i don't want to do that because i'm actually targeting one particular class because i i want to see when that connection object is getting creative my main intention to deal with the connection of that so i why i need to unnecessarily run this method before and after of each uh point card expression that i'll be writing but you can write a point cut and can only target this class this get connection method that's absolutely fine i just wanted to have this yeah yes yes obviously obviously point guard also can write only for this class right but this but i'm writing directly over here um now let's come back so this question yeah line number 18 you're returning the value that is for the report the service method [Music] oh in the log that is the connection inpull object um okay because this will be the get connection method right yes get connection method of which class data source what the get connection method will return it will return a connection object what is this with the connection is the interface who is who is giving the implementation mysql connection impul right that object is here now you know that inside the connection input right now it will use that connection input class so control sub t go to that connection impul class now it will use this class commit method to commit a transaction let's say get transaction method do i have get current get transaction to get the transaction do i have close method to close the connection i want to track when the transaction is getting committed when the transaction is getting called because obviously the connection input object will be used right so the way i'll be logging and tracking when that is happening by using i will will i will not pass the call directly to this class what i'll do i'll establish a proxy in between the my client my service whenever it will be using the connection object and before it makes a direct call to this object i will be establishing a proxy tracker which is going to track each information when the call will happen to this guy so the way i'll be doing it i know this is the connection object right so here i will be removing this guy not needed so this is a connection object so what i can do i can just do um i can create a proxy how can i create a proxy i told you right proxy dot new proxy instance i'll be creating a class loader class loader which is my impul class connection input so i'll be writing connection import i'll do connection impul dot class dot get class loader okay i need this thing next thing i need what what is the interface i told you this this one right how to create a dynamic proxy the next one will be the interface so new class and this is going to be the class interface the cloud sorry the class object here in i want to track connection this is the interface dot class okay the next thing is my invocation handler okay i'll be creating an invocation handler out of what this proxy will be created right so let's do one thing let's create a new invocation handler which will be a connection invocation hello to track the uh connection object when it is getting invoked it will go through that method so i'll do one thing inside my aop where is my aop stuff here i'll do new class and i will name the class name as connection invocation handler and i'll go over here and inside this class what i will be doing i will be implementing an interface called invocation handler i will not pass the call directly to the connection object i will have a invocation handler and i will be tracking because right now i'll be have a i'll have a hold to the connection object so i'll have my target object which will be private connection and connection this is going to be my target object now i will be initializing this one constructor using field i will initialize this one and then i will be doing connection i will invoke it here connection uh okay i'll invoke the connection method let's say method dot invoke and my target will be connection this is the object i'll be giving him and the argument shall be passing right and whatever it will return assign statement i will return this let's say return value and i will be returning it over here but here i am passing the call but before this i'll be tracking which method is getting called is it commit method is it rollback method is it a closed method i'll be tracking those things over here i'll be doing says out and i will be tracking it so i'll be writing this out and i will say connection trace and i'm gonna be tracking it so i'll be saying let's say method dot get name or maybe you can say uh to uh generic string so it's going to give me the method string or the generic string will be good because it is gonna give you the complete method definition which is going to print the entire information about the method which is getting called it a closed method is getting called or whether it is a commit method is getting called and all these things and now the main important thing is that we're using this connection object right this connection is not initialized but we have this constructor so what we'll be doing will be going to our expect and whenever we're creating the proxy here we'll be doing new uh connection invocation handler and will be passing in the connection object we know this return value will be a connection object so i can just pass this return value right here return value and i know this is going to be a connection object okay right so i think this is making sense this is a proxy and maybe i'll do one thing i'll just i'm type casting it over here i know it will it will always be a connection object because we are tracking this data source whenever the get connection method will be called it will give you the connection object but that connection object i'm intercepting because you know i'm creating a proxy by using this handler and right here i'll be tracking the thing and maybe this is going to be returning me a connection object so i'll say connection okay con is equal to and this proxy will return me a object so i'll be casting to a connection object and this con i'll return it over here and if you want more type septi if you if you're not sure okay maybe this can be something else it will not be something else it will be always connection because this is going to give you a connection but if you want to be make sure you can just do if a return value is a instance of connection okay then only you can do this thing okay and you can wrap this out like this and you can return the con right here okay or else you can return the value right here it's always going to go inside the if clause only okay now coming back to the point can i go to the invocation handler here i am tracking the method right now let's see which method is getting called here now do a control s go to the database first let me just run this and remove all this thing from here do i apply apply and close and now what i'll do here i will be basically running my application and now you will see something okay now look at the trace and here you can see first the get connection is getting called data source dot get connection before the self product info method got called if you remember the save product info is this one right before this method got called we got a transactional annotation now what will happen now we are tracking by using this uh invocation handler that we have created you remember every method that will be called on top of your connection object i'm tracking it i'm tracing it right here and now look at that and now here you can see first of all the get connection method got called then the auto commit got set this the the this is this is not throwing an exception this is the method which is getting called i'm just printing out the complete method signature this white saying throws this exception you are only printing the method then it will not print this will not print this particular exception part now we can see the auto commit is setting okay now the product info is getting called then all the products are getting saved here here here here here all the products are getting saved and now at last the commit will happen and it will close the connection and in between you can also see some logs like every time the set product is getting called before the product has been saved it is preparing a statement by using the prepared statement method okay the method that you have given internally will use the raw jdbc code only then the update method that you have written here inside the repo where is the repo are you finding the repo here here the update method will internally prepare this statement right that's why you can see before it save the product it is preparing the statement using this guy and you can look at that right prepare statement using the statement that you are giving is preparing it saving the product and returning all means we have we should be logging in something somewhere so maybe can i go there to the product sir maybe i'll go to the connection info sorry connection input i can close it so data source connection invocation i'm not printing anything am i printing anything inside the data source aspect um return value con am i printing anything anywhere why it is giving me now somewhere where this null is coming from i should be printing it somewhere something system the join point return con return value annotation in the project i think you were using it before for product service and products in this class oh yeah there you go because this uh safe production for does not give us anything right that's why it is giving null so okay the return value is nothing for this one so we can now so we can can we close every classes because there are so many classes i'm getting confused here now can i go to the you know method and i think this is right now making sense well let's do one thing guys let me only filter out the get connection the only i want to see the comment method and close method there a lot of other methods are also there so it is printing actually everything can i go to the connection invocation uh handler it should be there inside the aop package right here so inside here instead of printing everything here i only i want to restrict it so i will do if let me just zoom it out if the method name okay method dot get name if this contains what are the methods are there i want to only i will be only taking this method name commit if the method name is commit then only i log it or i'll copy this and if the method name is let's say rollback i will be um you know printing it the method name if the method name is close you can see at last we have closed right where is that console at last we have the close i i don't want to pay this auto commit part i don't repeat this prepare statement part so that it will be something clean so i only want to print the log if the things matches if this only matches print it otherwise don't print the method thing right so only when it is commit a roll back or the close method is getting involved just trace it and otherwise don't press it right okay fine so now let me go to my sql workbench and now let me do resume like you know delete everything do we apply close can i go back to here and now let me run this let me just run the r5 and look at that okay now you only see the console i will not be asking you anything we don't we have the default propagation label look at that before the service method has been called asking for the connection all this then the product safe product method got called which is a repo method but inside the repo what we have can i go to the repo inside the repo we have a transactional it will not create a new transaction because the default propagation label is what required it will use the existing transaction and that's what you can see there is no new connection has been created and once all the product has been saved it is doing a commit here and then once the commit has been done it is it is going to do a close here and this is what i have taught you right then i have taught you the right thing you can see over here you can go to the data source service method and inside the service i told you the same thing it will start the transaction here it will commit it and close it now tell me that guys from the trace is it clear or is not clear and one more thing guys right now let's do a another test now i will go to the uh let's say i'll go to here and i'll say this is maybe i will just go to i i will just do one thing i will comment out this a transactional from here from the service method then what will happen then the shareproduct method is with a transactional annotation then what will happen then it will create a new transaction here because this is the default propagation level is required and if it is required that means if the transaction is already there it will use the same transaction but if the transaction is not there it will create a new one if it is not there that means right now i am just commenting out this transactional here that means the new transaction will not be created in this point of time previously what used to happen before the sep product info method before this method right the transaction used to be created here right the transaction used to be created here get connection method but right now it will happen now the transaction will not be created in this point of time because i have commented out the transaction the transaction will be created before this one because the new because there is no new transaction exist and after this like before this method only the transaction will be created let me see whether that is happening let me just run this comment this out and delete this one do apply apply close can i go to here and let me run this example one more time and let's look let's look what is going to happen look at that before this method there is no new transaction got called but before the save product method say product method is our what it is our repo method before this a new transaction got created right and you can see right now before this a new transaction is got created here get connection method transaction has been saved then the safe product has been completed and then as this method is completed it is committing the transaction it is closing the transaction then again the for loop will call the seb product method if you remember inside the service the loop will go on again and again it will keep calling the sep product method and by the time it will create a new set product method again a new transaction will be created that means if something goes wrong here no guys the previous data will not be rolled back right because each time because everything has been treated as this different different transaction you are inserting 10 different products you can see now every time it is closing it is completing the method it is committing and closing the connection and again the for loop is calling the self-product method again it is creating a new connection in new transaction product is getting saved method is completed for safe product which is your dow method then again the commit will happen the close will happen right and then again before again the step product will be called by the for loop again the get connection method will be called right then the method will be saved then again the commit will happen the close will happen so each time the for loop is running right each time the for loop is running here the safe product method is creating new new transaction why because we are calling it again and again right and this is right now not finding any new transaction so it is creating one one transaction for each method call is it making sense to you right now what i'm trying to say and why i have told you that don't give a transactional here rather give a transactional here right is it making sense if you are using if you are using at transactional only maintain it inside the same transaction right now this will be simple the transaction will be created over here okay here only the transaction will be created the for loop will run for 10 times whenever this method will call every time it will say okay the transaction already exists because that that method only propagating to this method that method only is calling to this method so every time this method will be called and the same transaction will be used and once the invocation will complete till here the commit and close will happen in the end of the method if the transactional method exists over here right because the default propagation label is required is it making sense to you guys yes or no okay thank you okay make sense but only one question related to proxy i have when you use proxy instance to track the connection object but by like you can also use target within the target also you can mention in the connection here class and drag the methods there just like can we do it like that see if i am not doing anything with the connection input object i only want to track a kill that's what you want to lock it when it's printing when it is not printing so it can't be used with the target within the target you can mention connection impel class or something like that without and you can just you can just do ah you can do that but that's because i want to track every method okay how easy it is going to be with the invocation handler okay look at that i'm creating a proxy for the uh connection input class right here and this is the best interfaces connection included the invocation handler i'm going to track each method that over here and i'm just filtering it over here and i'm not doing anything with the you know connection method whatever the method they are giving they would have directly invoke it but right now i have used method dot invoke method to invoke it so whatever the business logic they have written that will be done i am just invoking that method but before that i want to track something and in the next classes you can see i'm going to add some more code to over here so that it will be more easy to understand the transactional annotation different propagation label license and level and the other things that we have because we are using this process yeah similarly i was asking you can do it you can do it but i'm gonna add some more code here okay and this is going to be the easiest approach this code makes sense to you right now okay whatever i have written yes yes yeah so guys the code makes sense tell me honestly like i have already told you dynamic proxy is the prerequisite so is it understandable right now honestly please tell me nagasi lavanya pankas prashanth or you know you can do practice if you haven't done practice you can do practice yeah but present the code makes sense to you yeah like i'm getting the idea yeah i'm getting the idea like what you're trying to do yeah tell me yes and no the code makes sense to you okay sure 11 yeah yeah one more time okay perfect so do that practice bankers are saying yourself make sure you are practicing the dynamic proxy part first okay um i am not mad that you know i'm giving you some prerequisite by wasting my time and energy before i would have directly write this class right i would have directly implemented this class in the first class itself i have taken time to teach you the dynamic proxy because there is something i'm going to do with that so make sure that aop first time learning yeah no problem you know punkers you can take your absolute time to learn aop in your repository in your website playlist um though in go don't go to the spring boot uh playlist go to the spring and hibernate bootcamp that you got in your website you can see there are some lessons in aopa are already there go through those things if you are new to aop all this thing whatever whatever we have discussed also will make sense to you it can only practice that much and in the spring boot section later on i'll be doing more and more aop stuff that will make more sense okay perfect now we have different uh level we will be learning in the next class we got required we got a lot of different things required we got supports we got mandatory we got not supported we got required new so you can actually we also got never we also got nested we're going to be talking about it in the next class you can also look just reverse the video look at the things that i have written and you know while you are practicing just think about it if you are able to get it but any anyhow if you're gonna able to write this particular class anyhow i'll give you the code you can if you're new to aop you just copy paste these three things over here and start tracking the things i've already written the code but you can do what you can play with it like in the service class max write your like in the repo class you can go here and um here you can write required propagation label and you can write never right so what it is never will do never means never means what look at the slide never means execute non-transactionally throw an exception if a transaction exist that means this piece of code need to be executed non-transactionally it should not follow any transaction we should not keep this code under any transaction and if there is a transaction exist already then throw an exception and in this case if you see here in the service class you've got a transactional annotation already so a exception will be thrown here in this case so right now if you're gonna do like run this let me save everything okay product reports have it if i'm gonna be saving this and running it it will throw you an exception look at that existing transaction found for the transaction marked with propagation label never so if you want to use and never if there is a already a transaction exist you remember the transaction has been created here right so the transaction exists so you should comment this out okay then only this will work now now this will work now do i have any data here let me just remove it apply apply close go over here and run this you can see now it will not give you any exception because there is no transaction exist look at that okay look at that how these things are getting executed and this is not maintaining any transaction here right so uh if you don't if you have a transaction let's say if you have a transaction and you don't want to comment this out but you want to make sure if there is any method um you you don't want to actually have a transaction for that you can write a transactional and instead of never you can say not supported not supported means for this method that we are not supporting any transaction and if there is any transaction present already and any method which is calling this method if there is a transaction present already make sure to suspend that particular transaction can we go to the um execute non-transactionally suspend the current transaction if one exists so if there is one transaction exist suspend it run this one right click f5 run this now you're going to see what is happening look at that we got a connection over here self product info before that we have written a transactional but when the safe product method got called look at that what is happening it did not create any transaction this is created just to save a product right you can see it is just closing it is not committing anything right there is no commit is getting caught for everyone it is just saving it non-transactionally by opening a connection and just closing it you can see it's just closed opening the connection uh saving the product and closing the connection no commit is happening right there is no commit is happening the commit will happen in the end of the method but you can see the end of the method of commit will happening because inside the product service we have a transactional obviously this this is automatically the propagation level is required right so commit and close will happen here but whenever you are calling this method we are executing this code non-transactionally if you have thousands lines of code we are saying that non-supported that means if there is any active transaction is there whenever this method is getting called make sure that to cancel the transaction or some suspend the transaction then only execute this code non-transactionally okay just like this you can write mandatory so will last one questionnaire is not supported if any exception occurs so it will not roll back it will not roll back because when we're at we're using it we are running the code non-transactionally right if you are using mandatory that means what mandatory means we should have a transaction if we don't have a transaction already then what will happen now look at that okay uh neighbor mandatory mandatory means support a current current transaction throw an exception if none exist so you need to have a transactional annotation in a service class okay if this one is not there okay and you are having mandatory here that means an exception will be thrown can i run this look at that illegal transaction transaction state exception non-existing transaction found for the transaction market propagation level mandatory and if you want to make it work make sure that you will go back to the service create the transaction here and whenever this method will be called the transaction need to be mandatory here right so now in f5 run it this should work fine you can see get the connection now the safe reduct method is acquiring the transaction created by this guy and at last it is committing it and closing it this is what we mean by a mandatory right and just like that you have uh various different level right mandatory i have told not supported i have told support also there is another one called support support a current transaction execute non-transactionally if non-exist that means okay no problem you can just go to over here and here you can write support or this is the service right this is the repo here you can change it to support support means if there is a transaction exist then use that transaction if the transaction does not exist just use non-transactionally right in this case there is a transaction exist right the transaction will be created over here so the support will be using that transaction right f5 run this look at that data connection is getting created over here the dow method is having the data doing all these sort of things committing it closing it right and now what if if this transactional is not active then it will just you know this will not throw any exception it will be done non-transactionally right now can i just run this now i can say to not throw us any exception okay but it is right now there is no transaction is happening so by the time it is having a product it is just opening the connection for the dow method and once the product method is completed disclosing the connection so no commit is happening so automatically uh like no non-transactionally the things are happening and at last also once the safe product info which is your service method will be completed there will not be any commit thing will be done because we don't have any transactional annotation on top of the subproduct method sorry uh inside the safe product info method we have commented this out making sense okay yes yes mandatory when you use mandatory will only commit at the end right with the same product correct mandatory mandatory will just use the existing transaction that i have written support a current transaction okay and throw exception if non exist required will do what it will be sorry what kind of exception it will be illegal transaction exception okay the transaction is there is no transaction like you know exist yeah yeah go ahead yeah i just was saying that like for uh when this like delete update like say we should like mark it as mandatory yeah obviously yeah obviously yeah so basically it depends uh basically it depends uh prashanth what kind of method you have required is the common one right required is the common one but all the level we have suppose we have support a current transaction execute non-transactionally if non-exist mandatory support a current transactional current transaction throw an exception if non-transaction exists not supported outside told execute non-transactionally so transaction need there should not be any transaction if one pound suspend the current suspend the current transaction if one exists if you don't want to throw any exception if you want to throw exception then you go go for mandatory right uh not supported means suspend the current transaction uh if one exists and just execute non-transactionally never sorry you can just go for neighbor and mandatory they are pretty straight forward name mandatory mean the transaction need to be there okay if not they are throw an exception never means or execute non-transactionally throw an exception if the transaction exists if some method is calling another method method a is calling method b but method a already have a transaction and in the method b you have written a transactional never that means method b will throw exception that there should not be any transaction exist right and just go with all these things just call all the method using the transaction here and without using the transaction here like you know you can just use the transaction here comment this out and uncomment it and then call the method and you just try out different uh propagation label over here and we will discuss more about this in the next class and we'll go back to the isolation level and the other advanced of uh lavinia jta platform manage jetty a platform manager the other stuff you are asking all right those things we can go for okay okay making this these are making sense lavender like you know like you know it's getting clear yes yes okay perfect all right nagasri uh any questions if you have laksmi sunivasar if you have any questions so mik i needed to practice yeah please do one time legally you don't have to practice you got too many years of experience just do it just say my code you'll understand yeah so i'll be asking for the code because i have not pretended yesterday's class no problem i've given already know don't talk to me don't prefer my interview go and sit whatever will happen that will happen you already learned so many things right no need to prepare for anything right yeah but i wanted to keep myself trapped like only only just do the prac do the coding practice like you know the logical question practice this thing they will ask and this thing you don't have to learn it by heart if you're gonna remembering it now guys it will not help you let me tell you if you are coding it if you are using it in your project then only this will make sense that's why make sure to code it and practice it and see it i don't remember all these things you know what i did before the class i opened the spring documentation written all this slide now see i'm reading the slide i'm testing it over here and in my code also i'll be doing the same thing now why i need to learn the things tell me you got documentation you know how to code you know spring just use use the knowledge use the documentation that's it okay i don't know anything guys i'm just reading the documentation and teaching you guys right and just whatever ktm giving you every day they are just from documentation and maybe something if i don't have i go to stack overflow and check okay how they have written all these things and how can i get some more idea and they are the documentation link they will be providing if you're not finding the same thing in the documentation go to um go go to the other links they're providing and look for those code code you need to see if you're gonna seeing the definition nothing will help right because end of the day you have to code this thing right it's not like you know somebody will ask you like you know what is what is propagation support yeah they'll ask you in the interview but end of the day you only need to decide when to put this when not to put this your uh where friend that you are getting from your client will clearly say this bunch of code does not need any transaction so you know okay i will use propagation label never in that particular place why should i use for default transactional annotation which is required all the time you should have this idea right so make sure these things only you are doing and apart from that interview wise practice the code maybe some core java questions some logical question you practice crack some good product based company and settle them settle in there right but don't settle for more time settle for two three years take hike switch just kidding yeah i have a question yeah within the same class if you have two methods right no this depends like what propagation level you are having no there is no propagation level it is required only yeah so why does it transition yeah why should it why should it give error okay it gives error it was saying you cannot call me thread just just just support the maybe i'm not able to understand it put the code in the group okay i'll be checking okay okay do it no you'll practice you'll be practicing no excuses for this one you do it and put the code base i'll come back and i'll check i'll go outside guys and and you know i'll check it um yeah so that's it so in the next lesson we'll be talking more about that transactional will keep going on so you are getting interest in this topic a transaction because very important guys if you are not getting interest also i'll be keep talking uh i i don't bother if you are getting interest or not two people are attending still i'll be talking about this topic only because i know the value of this one so make sure i i'll be making sure that this topic are they are on the repository in your playlist the paid playlist that you have purchased you have spent uh like you know 20 21 000 22 000 at least you get some value out of it right yeah okay i guess like if we don't understand this topic at the transactional transaction right yeah we can create a big loss to company yeah yeah i have been lost yeah i have been lost you you've already got experience from cause you have you have been building e-commerce site i know you understand the value of this though i remember the first day in company as a fresher and i got the transactional isolation level i thought i thought like you know what the heck is this thing okay so i started calling my friends my senior then you know i sat down i remember in one time saturday i was just doing a lot of research on the isolation level and then i got to know a few different things and unfortunately i'll be able to solve that particular story and i survived right so things happen with everyone so make sure that before the situation comes be prepared okay all right so guys i need to go out no classes no class tomorrow uh that's why i just want to sleep in the morning i'll stop the recording first i'll do the stop the recording first guys the next class whoever is listening to the recording will be on uh tuesday tuesday yeah tuesday to friday okay uh stop recording stop recording start let me also stop the camtasia recording stop recording um take care thank you thank you prashant take care yeah thank you thank you thank you guys bye [Music] you
Info
Channel: Selenium Express
Views: 25,017
Rating: undefined out of 5
Keywords: transactional propagation, spring boot, @Transactional, transaction management in spring, transaction management in spring boot, spring transaction, @transactional annotation, @transactional annotation in spring boot example, Spring transaction management example, selenium express, spring boot transaction commit, spring boot transaction management, spring boot transaction rollback example, spring boot transaction propagation, spring boot transaction management with jpa, abhilash
Id: IVHcWTegWyM
Channel Id: undefined
Length: 117min 56sec (7076 seconds)
Published: Sun Aug 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.