2 package OpenILS::Application::Storage;
3 use OpenSRF::Utils::Logger;
4 my $log = 'OpenSRF::Utils::Logger';
6 my $pg = 'OpenILS::Application::Storage::Driver::Pg';
12 return $pg->current_xact_id;
14 __PACKAGE__->register_method(
15 method => 'current_xact',
16 api_name => 'open-ils.storage.transaction.current',
22 sub pg_begin_xaction {
26 if (my $old_xact = $pg->current_xact_session) {
27 if ($pg->current_xact_is_auto) {
28 $log->debug("Commiting old autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
29 $self->pg_commit_xaction($client);
31 $log->debug("Rolling back old NON-autocommit transaction with Open-ILS XACT-ID [$old_xact]", INFO);
32 $self->pg_rollback_xaction($client);
33 throw OpenSRF::DomainObject::oilsException->new(
35 status => "Previous transaction rolled back!",
40 $pg->set_xact_session( $client->session );
41 my $xact_id = $pg->current_xact_id;
43 $log->debug("Beginning a new trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
45 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
52 $log->debug("Failed to begin a new trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
57 my $death_cb = $client->session->register_callback(
59 __PACKAGE__->pg_rollback_xaction;
63 $log->debug("Registered 'death' callback [$death_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
65 $client->session->session_data( death_cb => $death_cb );
67 if ($self->api_name =~ /autocommit$/o) {
68 $pg->current_xact_is_auto(1);
69 my $dc_cb = $client->session->register_callback(
72 $ses->unregister_callback(death => $death_cb);
73 __PACKAGE__->pg_commit_xaction;
76 $log->debug("Registered 'disconnect' callback [$dc_cb] for new trasaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
77 if ($client and $client->session) {
78 $client->session->session_data( disconnect_cb => $dc_cb );
85 __PACKAGE__->register_method(
86 method => 'pg_begin_xaction',
87 api_name => 'open-ils.storage.transaction.begin',
91 __PACKAGE__->register_method(
92 method => 'pg_begin_xaction',
93 api_name => 'open-ils.storage.transaction.begin.autocommit',
98 sub pg_commit_xaction {
101 my $xact_id = $pg->current_xact_id;
105 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
106 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
111 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
115 $pg->current_xact_session->unregister_callback( death =>
116 $pg->current_xact_session->session_data( 'death_cb' )
117 ) if ($pg->current_xact_session);
119 if ($pg->current_xact_is_auto) {
120 $pg->current_xact_session->unregister_callback( disconnect =>
121 $pg->current_xact_session->session_data( 'disconnect_cb' )
125 $pg->unset_xact_session;
130 __PACKAGE__->register_method(
131 method => 'pg_commit_xaction',
132 api_name => 'open-ils.storage.transaction.commit',
137 sub pg_rollback_xaction {
140 my $xact_id = $pg->current_xact_id;
144 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
145 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
150 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
154 $pg->current_xact_session->unregister_callback( death =>
155 $pg->current_xact_session->session_data( 'death_cb' )
156 ) if ($pg->current_xact_session);
158 if ($pg->current_xact_is_auto) {
159 $pg->current_xact_session->unregister_callback( disconnect =>
160 $pg->current_xact_session->session_data( 'disconnect_cb' )
164 $pg->unset_xact_session;
168 __PACKAGE__->register_method(
169 method => 'pg_rollback_xaction',
170 api_name => 'open-ils.storage.transaction.rollback',
178 my $sp = shift || 'osrf_savepoint';
179 return OpenILS::Application::Storage::CDBI->db_Main->pg_savepoint($sp);
181 __PACKAGE__->register_method(
182 method => 'set_savepoint',
183 api_name => 'open-ils.storage.savepoint.set',
188 sub release_savepoint {
191 my $sp = shift || 'osrf_savepoint';
192 return OpenILS::Application::Storage::CDBI->db_Main->pg_release($sp);
194 __PACKAGE__->register_method(
195 method => 'release_savepoint',
196 api_name => 'open-ils.storage.savepoint.release',
201 sub rollback_to_savepoint {
204 my $sp = shift || 'osrf_savepoint';
205 return OpenILS::Application::Storage::CDBI->db_Main->pg_rollback_to($sp);
207 __PACKAGE__->register_method(
208 method => 'rollback_to_savepoint',
209 api_name => 'open-ils.storage.savepoint.rollback',
220 return undef unless ($pg->current_xact_session);
222 my $cdbi = $self->{cdbi};
224 my $pri = $cdbi->columns('Primary');
226 my @cols = grep {$_ ne $pri} $cdbi->columns('All');
228 my $col_list = join ',', @cols;
230 $log->debug('Starting COPY import for '.$cdbi->table." ($col_list)", DEBUG);
231 $cdbi->sql_copy_start($cdbi->table, $col_list)->execute;
233 my $dbh = $cdbi->db_Main;
234 for my $node ( @fm_nodes ) {
236 my $line = join("\t", map { defined($node->$_()) ? $node->$_() : '\N' } @cols);
237 $log->debug("COPY line: [$line]",DEBUG);
238 $dbh->pg_putline($line."\n");
241 $dbh->pg_endcopy || $log->debug("Could not end COPY with pg_endcopy", WARN);
243 $log->debug('COPY import for '.$cdbi->table." ($col_list) complete", DEBUG);
245 return scalar(@fm_nodes);