@@ -126,38 +126,41 @@ def ispyb_session_factory(ispyb_engine):
126126def ispyb_db (ispyb_session_factory ) -> Generator [SQLAlchemySession , None , None ]:
127127 # Get a new session from the session factory
128128 ispyb_db : SQLAlchemySession = ispyb_session_factory ()
129-
130- # Populate the ISPyB table with some default values
131- person_db_entry = Person (
132- givenName = ExampleVisit .given_name ,
133- familyName = ExampleVisit .family_name ,
134- login = ExampleVisit .login ,
135- )
136- ispyb_db .add (person_db_entry )
137- ispyb_db .commit ()
138-
139- proposal_db_entry = Proposal (
140- personId = person_db_entry .personId ,
141- proposalCode = ExampleVisit .proposal_code ,
142- proposalNumber = str (ExampleVisit .proposal_number ),
143- )
144- ispyb_db .add (proposal_db_entry )
145- ispyb_db .commit ()
146-
147- bl_session_db_entry = BLSession (
148- proposalId = proposal_db_entry .proposalId ,
149- beamLineName = ExampleVisit .instrument_name ,
150- visit_number = ExampleVisit .visit_number ,
151- )
152- ispyb_db .add (bl_session_db_entry )
153- ispyb_db .commit ()
154-
155- # Yield the Session and pass processing over to other function
156- yield ispyb_db
129+ save_point = ispyb_db .begin_nested () # Checkpoint to roll back database to
130+
131+ try :
132+ # Populate the ISPyB table with some default values
133+ person_db_entry = Person (
134+ givenName = ExampleVisit .given_name ,
135+ familyName = ExampleVisit .family_name ,
136+ login = ExampleVisit .login ,
137+ )
138+ ispyb_db .add (person_db_entry )
139+ ispyb_db .commit ()
140+
141+ proposal_db_entry = Proposal (
142+ personId = person_db_entry .personId ,
143+ proposalCode = ExampleVisit .proposal_code ,
144+ proposalNumber = str (ExampleVisit .proposal_number ),
145+ )
146+ ispyb_db .add (proposal_db_entry )
147+ ispyb_db .commit ()
148+
149+ bl_session_db_entry = BLSession (
150+ proposalId = proposal_db_entry .proposalId ,
151+ beamLineName = ExampleVisit .instrument_name ,
152+ visit_number = ExampleVisit .visit_number ,
153+ )
154+ ispyb_db .add (bl_session_db_entry )
155+ ispyb_db .commit ()
156+
157+ # Yield the Session and pass processing over to other function
158+ yield ispyb_db
157159
158160 # Tidying up
159- ispyb_db .rollback ()
160- ispyb_db .close ()
161+ finally :
162+ save_point .rollback ()
163+ ispyb_db .close ()
161164
162165
163166"""
0 commit comments