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;
104 $log->debug("Committing trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
105 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
110 $log->debug("Failed to commit trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
114 $pg->current_xact_session->unregister_callback( death =>
115 $pg->current_xact_session->session_data( 'death_cb' )
116 ) if ($pg->current_xact_session);
118 if ($pg->current_xact_is_auto) {
119 $pg->current_xact_session->unregister_callback( disconnect =>
120 $pg->current_xact_session->session_data( 'disconnect_cb' )
124 $pg->unset_xact_session;
129 __PACKAGE__->register_method(
130 method => 'pg_commit_xaction',
131 api_name => 'open-ils.storage.transaction.commit',
136 sub pg_rollback_xaction {
139 my $xact_id = $pg->current_xact_id;
141 my $dbh = OpenILS::Application::Storage::CDBI->db_Main;
142 $log->debug("Rolling back a trasaction with Open-ILS XACT-ID [$xact_id]", INFO);
147 $log->debug("Failed to roll back trasaction with Open-ILS XACT-ID [$xact_id]: ".$e, INFO);
151 $pg->current_xact_session->unregister_callback( death =>
152 $pg->current_xact_session->session_data( 'death_cb' )
153 ) if ($pg->current_xact_session);
155 if ($pg->current_xact_is_auto) {
156 $pg->current_xact_session->unregister_callback( disconnect =>
157 $pg->current_xact_session->session_data( 'disconnect_cb' )
161 $pg->unset_xact_session;
165 __PACKAGE__->register_method(
166 method => 'pg_rollback_xaction',
167 api_name => 'open-ils.storage.transaction.rollback',
177 return undef unless ($pg->current_xact_session);
179 my $cdbi = $self->{cdbi};
181 my $pri = $cdbi->columns('Primary');
183 my @cols = grep {$_ ne $pri} $cdbi->columns('All');
185 my $col_list = join ',', @cols;
187 $log->debug('Starting COPY import for '.$cdbi->table." ($col_list)", DEBUG);
188 $cdbi->sql_copy_start($cdbi->table, $col_list)->execute;
190 my $dbh = $cdbi->db_Main;
191 for my $node ( @fm_nodes ) {
193 my $line = join("\t", map { defined($node->$_()) ? $node->$_() : '\N' } @cols);
194 $log->debug("COPY line: [$line]",DEBUG);
195 $dbh->func($line."\n", 'putline');
198 $dbh->func('endcopy');
200 return scalar(@fm_nodes);