use List::Util ();
__PACKAGE__->mk_group_accessors('simple' =>
- qw/_identity _blob_log_on_update unsafe_insert _insert_dbh/
+ qw/_identity _blob_log_on_update _insert_dbh _identity_method/
);
=head1 NAME
With this driver there is unfortunately no way to get the C<last_insert_id>
without doing a C<SELECT MAX(col)>. This is done safely in a transaction
-(locking the table.) The transaction can be turned off if concurrency is not an
-issue, or you don't need the C<IDENTITY> value, see
-L<DBIx::Class::Storage::DBI::Sybase/connect_call_unsafe_insert>.
-
-But your queries will be cached.
+(locking the table.) See L</INSERTS WITH PLACEHOLDERS>.
A recommended L<DBIx::Class::Storage::DBI/connect_info> setting:
if exists $args{log_on_update};
}
-=head2 connect_call_unsafe_insert
-
-With placeholders enabled, inserts are done in a transaction so that there are
-no concurrency issues with getting the inserted identity value using
-C<SELECT MAX(col)> when placeholders are enabled.
-
-When using C<DBIx::Class::Storage::DBI::Sybase::NoBindVars> transactions are
-disabled.
-
-To turn off transactions for inserts (for an application that doesn't need
-concurrency, or a loader, for example) use this setting in
-L<DBIx::Class::Storage::DBI/connect_info>,
-
- on_connect_call => ['unsafe_insert']
-
-To manipulate this setting at runtime, use:
-
- $schema->storage->unsafe_insert(0|1);
-
-=cut
-
-sub connect_call_unsafe_insert {
- my $self = shift;
- $self->unsafe_insert(1);
-}
-
sub _is_lob_type {
my $self = shift;
my $type = shift;
my $blob_cols = $self->_remove_blob_cols($source, $to_insert);
-# insert+blob insert done atomically
- my $guard = $self->txn_scope_guard if $blob_cols;
+# insert+blob insert done atomically, on _insert_dbh
+ (my ($guard), local ($self->{_dbh})) = do {
+ $self->_insert_dbh($self->_connect(@{ $self->_dbi_connect_info }))
+ unless $self->_insert_dbh;
+
+ my $new_guard = $self->txn_scope_guard;
+
+# _dbh_begin_work may reconnect, if so we need to update _insert_dbh
+ $self->_insert_dbh($self->_dbh);
+
+ ($new_guard, $self->_insert_dbh)
+ } if $blob_cols;
my $need_last_insert_id = 0;
# We have to do the insert in a transaction to avoid race conditions with the
# SELECT MAX(COL) identity method used when placeholders are enabled.
my $updated_cols = do {
+ no warnings 'uninitialized';
if (
- $need_last_insert_id && !$self->unsafe_insert && !$self->{transaction_depth}
+ $need_last_insert_id &&
+ $self->_identity_method ne '@@IDENTITY' &&
+ !$self->{transaction_depth}
) {
$self->_insert_dbh($self->_connect(@{ $self->_dbi_connect_info }))
unless $self->_insert_dbh;
local $self->{_dbh} = $self->_insert_dbh;
+
my $guard = $self->txn_scope_guard;
+
+# _dbh_begin_work may reconnect, if so we need to update _insert_dbh
+ $self->_insert_dbh($self->_dbh);
+
my $upd_cols = $self->next::method (@_);
$guard->commit;
- $self->_insert_dbh($self->_dbh);
+
$upd_cols;
}
else {
Inserts or updates of TEXT/IMAGE columns will B<NOT> work with FreeTDS.
+=head1 INSERTS WITH PLACEHOLDERS
+
+With placeholders enabled, inserts are done in a transaction so that there are
+no concurrency issues with getting the inserted identity value using
+C<SELECT MAX(col)>, which is the only way to get the C<IDENTITY> value in this
+mode.
+
+When using C<DBIx::Class::Storage::DBI::Sybase::NoBindVars> transactions are
+disabled, as there are no concurrency issues with C<SELECT @@IDENTITY> as it's a
+session variable.
+
=head1 TRANSACTIONS
Due to limitations of the TDS protocol, L<DBD::Sybase>, or both; you cannot
C<next> or C<first> but has not been exhausted or
L<reset|DBIx::Class::ResultSet/reset>.
+For example, this will not work:
+
+ $schema->txn_do(sub {
+ my $rs = $schema->resultset('Book');
+ while (my $row = $rs->next) {
+ $schema->resultset('MetaData')->create({
+ book_id => $row->id,
+ ...
+ });
+ }
+ });
+
Transactions done for inserts in C<AutoCommit> mode when placeholders are in use
are not affected, as they use an extra database handle to do the insert.
=item * load the data from your cursor with L<DBIx::Class::ResultSet/all>
-=item * enlarge the scope of the transaction
-
=back
=head1 MAXIMUM CONNECTIONS
my $dbh = $self->_dbh or return 0;
local $dbh->{RaiseError} = 1;
+ local $dbh->{PrintError} = 0;
+ local $@;
+
+ if ($dbh->{syb_no_child_con}) {
+# ping is impossible with an active statement, we return false if so
+ my $ping = eval { $dbh->ping };
+ return $@ ? 0 : $ping;
+ }
+
eval {
+# XXX if the main connection goes stale, does opening another for this statement
+# really determine anything?
$dbh->do('select 1');
};
$dbh->do("SET TEXTSIZE $bytes");
Takes the number of bytes, or uses the C<LongReadLen> value from your
-L<DBIx::Class/connect_info> if omitted.
+L<DBIx::Class/connect_info> if omitted, lastly falls back to the C<32768> which
+is the L<DBD::Sybase> default.
=cut
sub set_textsize {
my $self = shift;
my $text_size = shift ||
- eval { $self->_dbi_connect_info->[-1]->{LongReadLen} };
+ eval { $self->_dbi_connect_info->[-1]->{LongReadLen} } ||
+ 32768; # the DBD::Sybase default
return unless defined $text_size;
my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
-my $TESTS = 39 + 2;
+my $TESTS = 40 + 2;
if (not ($dsn && $user)) {
plan skip_all =>
});
# test insert transaction when there's an active cursor
- TODO: {
-# local $TODO = 'not supported yet or possibly ever';
+ SKIP: {
+ skip 'not testing insert with active cursor if using ::NoBindVars', 1
+ if $storage_type =~ /NoBindVars/i;
+
+ my $artist_rs = $schema->resultset('Artist');
+ $artist_rs->first;
+ lives_ok {
+ my $row = $schema->resultset('Money')->create({ amount => 100 });
+ $row->delete;
+ } 'inserted a row with an active cursor';
+ $ping_count-- if $@; # dbh_do calls ->connected
+ }
- SKIP: {
- skip 'not testing insert with active cursor if using unsafe_insert', 1
- if $schema->storage->unsafe_insert;
+# test insert in an outer transaction when there's an active cursor
+ TODO: {
+ local $TODO = 'this should work once we have eager cursors';
- my $artist_rs = $schema->resultset('Artist');
- $artist_rs->first;
- lives_ok {
+# clear state, or we get a deadlock on $row->delete
+# XXX figure out why this happens
+ $schema->storage->disconnect;
+
+ lives_ok {
+ $schema->txn_do(sub {
+ my $artist_rs = $schema->resultset('Artist');
+ $artist_rs->first;
my $row = $schema->resultset('Money')->create({ amount => 100 });
$row->delete;
- } 'inserted a row with an active cursor';
- $ping_count-- if $@; # dbh_do calls ->connected
- }
+ });
+ } 'inserted a row with an active cursor in outer txn';
+ $ping_count-- if $@; # dbh_do calls ->connected
}
# Now test money values.