How to change database from MySQL to Oracle in TheSchwartz - perl

I'd like to switch database from MySQL to Oracle in perl's TheSchwartz module which is the job queue system. I suspects following code has the key.
my $client = TheSchwartz->new(
databases => [{
dsn => 'dbi:mysql:TheSchwartz',
user => 'dbi',
pass => 'xxxxxxx',
}],
verbose => 1,
);
I changed the code.
dsn => 'dbi:mysql:TheSchwartz',
to
dsn => 'dbi:Oracle:OraDB01',
then I got the message.
#Disabling DB 9e410d44ac4b9ede28c9ef34f6c1e817 because unknown
TheSchwartz doesn't tell me any clue of Oracle's error(ex password error ,or network error ....).
My question are
1. Is it possible to use Oracle in perl's TheSchwartz?
2. For dubugging ,how to get the ORA- Error message in TheSchwartz?
Any help would be welcomed.
Regards,

Using TheSchwartz is almost certainly a complete red herring here. TheSchwartz is just creating a DBI connection. So once you can successfully create a DBI connection outside of TheSchwartz, you should be able to do the same insider TheSchwartz.
The way I would approach it would be in two stages:
Write a simple DBI program to connect to the Oracle database.
Copy your successful connection details to your TheSchwartz code.
The simple DBI program I'd write, only needs to be as simple as this:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect('dbi:Oracle:OraDB01', 'dbi', 'xxx')
or die $DBI::errstr;
That will presumably fail in the same way as your existing code, but you'll get the raw DBI connection error which should allow you to work out what the problem is.
Obviously, you might find the DBD::Oracle documentation to be useful as well.

Related

ODBC to SAP HANA connection timeout in Perl

Since I'm programming a Perl script to monitor the SAP HANA database, I'm experiencing problems with the connection timeout. I don't know how to set the timeout. This is my connection string:
use DBI;
use DBD::ODBC;
$dbh = DBI->connect("dbi:ODBC:dsn=$dsn");
Meanwhile in another script to monitor a Sybase database it works properly:
use DBI;
dbh = DBI->connect("dbi:Sybase:server=$dbsvr;database=$dbname;".
timeout=$timeout;"."loginTimeout=$timeout", $user, $pass);
I think the problem comes with the ODBC. Which is the way to implement a timeout using ODBC connection?
I don't believe there is a way to set SQL_ATTR_LOGIN_TIMEOUT on the connection handle via DBD::ODBC. In any case, few ODBC drivers do anything with it. However, if you are on UNIX and using the unixODBC driver manager - see Setting ODBC driver environment variables automatically
You basically add "DMConnAttr = CONNECTION_ATTRIBUTE=value" to you odbc.ini file where CONNECTION_ATTRIBUTE is an ODBC connection attribute (e.g., SQL_ATTR_CONNECTION_TIMEOUT) and value is what you want to set it to.
It is commom to confused with connection and query timeout:
DMStmtAttr = SQL_ATTR_QUERY_TIMEOUT=2
DMConnAttr = SQL_ATTR_CONNECTION_TIMEOUT=2
Connection may be ok but query response take more time than normal

MariaDB replication some times does not working

I have a problem that I have never seen before with Mysql replication (Master/Slave):
It works excellent when I execute on my PHP scripts, on the mysql console queries like "INSERT INTO". They are perfectly replicated on the mysql slave machine.
But: Not if I do this same operations on a Perl script, using Perl DBI. Then its only stored on the Mysql master server. In the mysql.log file on the slave server, nothing arrives. I have this problem since I migrated on the two servers from Mysql to MariaDB.
Is there something that a Perl DBI client should absolutely do that replicating would work?
I'm using this simple code to INSERT data:
#!/usr/bin/perl
use DBI;
$dsn = 'dbi:mysql:database=mysql;host=myhostname';
$dbh = DBI->connect($dsn, 'mouser', 'password',
{ RaiseError => 1, AutoCommit => 0 }) || exit(1);
...
$sth = $dbh->prepare("INSERT INTO mydatabase.mytable (user, domain) VALUES('$account_name', '$domain')");
$sth->execute();
$dbh->commit();
Is it possible that it does not replicate because DBI should use a mariadb driver? Like $dsn = 'dbi:mariadb:database=mysql;host=myhostname'; if that exists?
To be clear: Data inserted with your PHP script is still being replicated after switching to MariaDB, but data inserted with your Perl script isn't.
I found the reason why the replication failed with this simple Perl script:
There is written:
$dsn = 'dbi:mysql:database=mysql;host=myhostname';
...
$sth = $dbh->prepare("INSERT INTO mydatabase.mytable (user, domain) VALUES('$account_name', '$domain')");
This is not working, because DBI connects to the database "mysql" and not "mydatabase" (see "database=mysql"). Even if the INSERT INTO inserts the row directly in the database "mydatabase", MariaDB master server ignores it for the replication. Its strange that it worked perfectly with MySQL and not for MariaDB (version 5.5.46). Maybe its a bug in MariaDB, I don't know.

Perl script to connect oracle database

I am new to Perl Programming and database connectivity. Can anyone please let me know step by step procedure to write Perl Script for Oracle Database connectivity.
My Perl Verion is:
This is perl 5, version 22, subversion 0 (v5.22.0) built for MSWin32-x64-multi-thread
Copyright 1987-2015, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
I have tried the below:
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM") or die print ("could not connect! $DBI::errstr \n");
Since I don't know what is this "dbi:Oracle:Local" I could not able to connect to Database.
Can you please let me know what is dbi, Oracle, local. if it is Hostname and oracle database name, how can I find the same in my computer.
Do I need to set any ENV variable in Perl? If so, where and what I need to set?
dbi:Oracle lets DBI know which driver to use. If you're connecting to an Oracle database, you'll never change these.
Local is either an actual database name on the local system, or a name listed in TNSNAMES.ORA. Substitute the name of the local database you'd like to connect to.
The next two parameters are username and password.
If you're connecting remotely, or need to do something more elaborate, consult the docs, or one of the many available guides.
my $db = DBI->connect("dbi:Oracle:Local","SYSTEM","SYSTEM");
"dbi" is a string,
"Oracle" is the driver type,
"local" is the database name,
First "SYSTEM" is the username and second one is password.
use DBI;
my $dbh = DBI->connect( "dbi:Oracle:databaseName", 'username', 'passwd' ) or die($DBI::errstr, "\n");
First read the basic concept of Simple Database access using Perl DBI and SQL.

Connecting to a SQL Server DB

I’ve been trying to connect to a SQL Server DB in Perl since yesterday, and it’s a failure. I use the DBI->connect function, but I got error saying the data source and the driver are not found. I use that string:
'dbi:ODBC:mydb:myhost\myinstance'
I’m not really sure the string’s correct. Any idea?
EDIT: here’s the error I got:
DBI connect('***:***\***','',...) failed: [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified (SQL-IM002)
at Collector.pl line 137
The format of the DBI connect string for ODBC is 'dbi:ODBC:DSN_name' or 'dbi:ODBC:DSN=DSN_name' (for connections using Data Source Names (DSNs). In that case DSN_name needs to be the name of a data source you created with the ODBC administrator. This situation is slightly more complicated in Windows 64 bit environments as there are 2 ODBC administrators, one for 32 bit applications and one for 64 bit applications (you cannot mix the two).
So, first, work out if you Perl is 32 bit or 64 bit executable - you can usually see this from the output of perl -V (look for archname but there are other indications too). Then find the right ODBC Administrator depending on how your perl was built and create the data source with it. Lastly, change your DBI connect string as above.
There are also so called DSN-less connections that do not require a DSN but instead you name the DRIVER and any attributes required to connect to the database.
There differences in what DBD::ODBC does if you use 'dbi:ODBC:DSN_name' instead of 'dbi:ODBC:DSN=DSN_name' which you can read about ODBC - The Connection Process.
You can find out more about 32/64 bit ODBC and where to find the right driver manager at 64 bit ODBC. You can find out about DBD::ODBC and connections at Drivers, Data Sources and Connection - Perl DBI/DBD::ODBC Tutorial Part 1.
Well this seems to work ok,
use DBI;
#
$dbh = DBI->connect( "dbi:mysql:DBNAME", "root", "password" )
or die "Connection Error $DBI::errstr\n";
N.B. Please Check you have the required modules installed.

Perl DBI connection to PostgreSQL doesn't die() on database error

I am used to Perl "crashing" my script when a SQL goes bad, but Postgres is just giving the error to the Apache error log and the script continues. Evil behaviour for debugging code.
Now I have asked the Oracle of Google but either I am asking the wrong questions or, very unlikely, I am the only one with that problem. ;)
In Short: I want the same fault behaviour with Postgres as MySQL does, ie, "crash" the script and take heed of use CGI::Carp qw(fatalsToBrowser); and just show me the faulty SQL statement.
I am using good old DBI to access the PG-DB, Postgres 8.4 and Perl 5.10.1 on Ubuntu 10.04 on my server, if that matters. All packages are from the official rep.
EDIT:
Thanks to Richard Huxton this is solved, leaving the solution for others to find.
our $dbh = DBI->connect($DBUrl, $DBUser, $DBPass,{ RaiseError => 1}
) || die "Could not connect to database: $DBI::errstr";
Surely you just need to set RaiseError when you make the connection?
https://metacpan.org/module/DBI