Body parsing: Spring is the processing of @transactional annotations in the Createtransactionifnecessary method of Transactionaspectsupport (tangent programming). Then delegate the gettransaction () method to the Platformtransactionmanager implementation class (implementing Class is Hibernatetransactionmanager is configured in application.xml) the processing of Propagation=propagation.supports is also implemented within this method, Specifically, the parent class abstractplatformtransactionmanager.gettransaction (). The code snippet is as follows: Abstractplatformtransactionmanager.gettransaction (transactiondefinitiondefinition) { Object Transaction = Dogettransaction (); Note 1:springframework's Hibernatetransactionobject instance, which is held internally by Hibernatesession, is initially null; . . Othercode. . No existing transaction found, check Propagationbehavior to find out how to proceed. if (Definition.getpropagationbehavior () = = Transactiondefinition.propagation_mandatory) { throw New Illegaltransactionstateexception ( "No existing transaction found for transactionmarked with propagation ' mandatory '"); } else if (Definition.getpropagationbehavior () = = Transactiondefinition.propagation_required | | Definition.getpropagationbehavior () ==transactiondefinition.propagation_requires_new | | Definition.getpropagationbehavior () ==transactiondefinition.propagation_nested) { Suspendedresourcesholder suspendedresources = suspend (null); if (debugenabled) { Logger.debug ("Creating New transaction Withname [" + definition.getname () + "]:" + definition); } Try { boolean newsynchronization = (gettransactionsynchronization ()! = Synchronization_never); Defaulttransactionstatus status = Newtransactionstatus ( Definition, transaction, True, Newsynchronization, debugenabled, suspendedresources); Dobegin (transaction, definition); Note 2: Generate Hibernatesession, then get jdbcconnection, and Connection,setautocommit (false), Open the SQL transaction that we understand. Specifically, call the hibernatejdbctransaction in the. Begin () method in the session holding Preparesynchronization (status,definition); Set Springtransactionsynchronizationmanager.initsynchronization (). return status; } Catch (RuntimeException ex) { Resume (null, suspendedresources); throw ex; } Catch (Error Err) { Resume (null, suspendedresources); throw err; } } Else { Note 3 This process is the path that is executed when there is no transaction in the thread and the configuration is propagation= propagation.supports. This step does not generate Hibernate Session, JDBC Connection. But Preparetransactionstatus() Does a step, setting springtransactionsynchronizationmanager.initsynchronization (). It is only after this operation that we are able to allow Hibernate callback to initialize the Spring Springsessioncontext generated hibernatesession when hibernate save or update occurs, Further getting jdbcconnection from the connection pool Create "Empty" Transaction:no actualtransaction, but potentially synchronization. boolean newsynchronization = (gettransactionsynchronization () = = Synchronization_always); return Preparetransactionstatus (definition, null, true, Newsynchronization, debugenabled, null); } . . . } Note 2 and note 3 answer 2, 3 questions. A little bit of the idea of naming in the above code: L Gettransaction return to Transactiostaus, feel more appropriate or named Gettransactiostaus than Spring's current naming gettransaction Gettransactionifnecessa Ry is better, according to note 3, this branch does not actually open the database transaction. Not even get JDBC Connection. And any generated Hibernate Session. L Compare important Several classes of Springframework Hibernatetransactionobject,hibernate's session,hibernate jdbctransaction, The connection of JDBC. Springframework Hibernatetransactionobject holds a hibernate session, Hibernate session holds a hibernate jdbctransaction, Eventually holds the JDBC Connection. Because the database transaction Connection.setautocommit (FALSE). Transaction opening is attached to connection. The connection should be open before the transaction. So it feels different to use springframework Hibernatetransactionobject and hibernate jdbctransaction to hold JDBC connection. Named Springframework Hibernatetransactionstatusobject, Hibernate jdbctransactionstaus more reasonable. For a 4th question. For @transaction (Propagation=propagation.require), session new and session acquisition are The Abstractplatformtransactionmanager.gettransaction method completes the For @transaction (propagation=propagation.supports), lazy gets connection when lazy gets the session, and only when the database operation is actually attempted. The simple stack that gets connection is as follows. Connectionmanager.openconnection () line:446//Note: Find no connection, get a connection from the thread pool Connectionmanager.getconnection () line:167 Batchingbatcher (Abstractbatcher). Preparequerystatement (String,boolean, Scrollmode) line:161 Entityloader (Loader). Preparequerystatement (Queryparameters,boolean, Sessionimplementor) line:1700 |