How to manualy commit or rollback in postgres ruby - ruby

I had some ruby code in a ruby script that connected to a mysql DB. When I connected I then did a MySQL2.query("BEGIN") to start a transaction and if I wanted to rollback I did MySQL2.query("ROLLBACK") or if all was well and I wanted to commit I did MySQL2.query("COMMIT").
I have now moved to a postgres database and while the PG.exec("BEGIN"), PG.exec("ROLLBACK") and PG.exec("COMMIT") do not seem to error I do get a warning 'there is no transaction in progress' when I commit so it seems it is doing auto commit (i.e. committing each SQL INSERT/UPDATE as it is done). Basically I want to be able to manually rollback or commit.
I think maybe I need to turn autocommit off but cant work out how, I tried #dbase.exec('SET AUTOCOMMIT TO OFF') but got the error 'lib/database.rb:28:in `exec': ERROR: unrecognized configuration parameter "autocommit" (PG::UndefinedObject)'.
Ive done a fair amount of googling without any luck;(.
I am using postgres 9.5 and ruby 2.4.1.

PostgreSQL does not have a setting to disable autocommit:
https://stackoverflow.com/a/17936997/3323777
You just should use BEGIN/COMMIT/ROLLBACK.
BTW which adapter gem do you use? PG.exec syntax seems strange.
Consider the following snippet (pg 0.20 used):
conn = PGconn.open(:dbname => 'database', user: "user", password: "...")
conn.exec("DELETE FROM setting_entries")
conn.exec("INSERT INTO setting_entries(name) VALUES ('1')")
conn.exec("BEGIN")
conn.exec("DELETE FROM setting_entries")
conn.exec("INSERT INTO setting_entries(name) VALUES ('1')")
conn.exec("ROLLBACK")
And this is the postgre log:
(0-22/70) 2017-08-31 12:37:12 MSK [19945-1] user#database LOG: statement: DELETE FROM setting_entries
(0-22/71) 2017-08-31 12:37:12 MSK [19945-2] user#database LOG: statement: INSERT INTO setting_entries(name) VALUES ('1')
(0-22/72) 2017-08-31 12:37:12 MSK [19945-3] user#database LOG: statement: DELETE FROM setting_entries
(5948637-22/72) 2017-08-31 12:37:12 MSK [19945-4] user#database LOG: statement: INSERT INTO setting_entries(name) VALUES ('1')
As you can see, the transaction ids are the same (/72) for the last two lines.
To be sure you could write a unit test where you would make two updates within a transaction, rollback them and see if both would roll back.

Related

ORA-24756: transaction does not exist

Am doing a distributed update and I get this ORA-24756 error:
DISTRIB TRAN bea1.67AA54355C4A74ECDEE0
is local tran 6.42.332492 (hex=06.2a.512cc)
insert pending prepared tran, scn=8151148567799 (hex=769.d6509cf7)
Wed Jan 30 16:45:02 EAT 2015
Errors in file /oradata/sfapdb/bdump/sfapdb_reco_2739.trc:
ORA-24756: transaction does not exist
oracle.jdbc.xa.OracleXAExceptionSQLException: ORA-24756: transaction does not exist.
This error is from a failed two-phase commit
I also found out there are some records (trans_id = "6.42.332492") in SYS.PENDING_TRANS$/ SYS.PENDING_SESSION$/dba_2pc_pending with "prepare" status. This transaction is launched from a Weblogic Server via JDBC.\
How do I resolve this ORA-24756 error?

Connect to Teradata Using Airflow JDBC Connection

I'm trying to execute a SqlSensor task in Airflow using a connection to Teradata database. The connection is configured as follow:
I have provide in particular 2 driver paths separated by ", " but I am not sure if it's the proper way to do it?
/home/airflow/java_sample/tdgssconfig.jar
/home/airflow/java_sample/terajdbc4.jar
When the DAG executes, it triggers the error message
[2017-08-02 02:32:45,162] {models.py:1342} INFO - Executing <Task(SqlSensor): check_running_batch> on 2017-08-02 02:32:12
[2017-08-02 02:32:45,179] {base_hook.py:67} INFO - Using connection to: jdbc:teradata://myservername.mycompanyname.org/database=MYDBNAME,TMODE=ANSI,CHARSET=UTF8
[2017-08-02 02:32:45,313] {sensors.py:109} INFO - Poking: SELECT BATCH_KEY FROM MYDBNAME.AUDIT_BATCH WHERE BATCH_OWNER='ARO_TEST' AND AUDIT_STATUS_KEY=1;
[2017-08-02 02:32:45,316] {base_hook.py:67} INFO - Using connection to: jdbc:teradata://myservername.mycompanyname.org/database=MYDBNAME,TMODE=ANSI,CHARSET=UTF8
[2017-08-02 02:32:45,497] {models.py:1417} ERROR - java.lang.RuntimeException: Class com.teradata.jdbc.TeraDriver not found
What am I doing wrong?
The appropriate way to input multiple jars in the connections page is to separate both fully qualified paths with a comma which you did above.
I can confirm this is the approach I took and it worked (Airflow 10.1.1 and 10.1.2).
See: https://github.com/apache/airflow/blob/master/airflow/hooks/jdbc_hook.py#L51
Bonus: If you use Ad Hoc Query in Data Profiling to test it out, you'll notice that you'll get an error when you send a SELECT statement because Airflow wraps it in a LIMIT clause which TD doesn't support.
The solution provided by my team member was to merge the two jar into a single jar file. After doing it and indicating that new jar file in the driver path, it worked as expected.
Here is the link to the JAR file: https://github.com/alexisrolland/linux-setup/blob/master/teradataDriverJdbc.jar
Here is a code snippet example to use the connection in a SQLSensor Task:
CheckRunningBatch = SqlSensor(
task_id='check_running_batch',
conn_id='ed_data_quality_edw_dev',
sql="SELECT CASE WHEN MAX(BATCH_KEY) IS NOT NULL THEN 0 ELSE 1 END FROM DATABASE.AUDIT_BATCH WHERE STATUS_KEY=1;",
poke_interval=300,
dag=dag)

Socket error - Connection reset by peer

I have a simple housekeeping session that deletes data prior to one month from teradata tables.This is done using SQL transformation.The mapping has logic such that it handles 3 types of files (daily , monthly first week ,monthly -last week).logic for all 3 types is same.Deleting data prior to 1 month.Router is used to handle this.
The session executes properly for low volume of data(daily file) but when the table has higher volume of data in production , it gives below error :
TRANSF_1_1_1> pmsql_50065 [ERROR] ODL error:
FnName: Close -- [Teradata][Unix system error] Socket error - Connection reset by peer FnName: Execute Direct -- [Teradata][Unix system error] 104 Socket error - Connection reset by peer ODBC call to SQLError failed. .
TRANSF_1_1_1> CMN_1761 Timestamp Event: [Fri Aug 08 09:45:01 2014]
We have tried re-executing the session but no luck. Weird part is the delete command is being executed in database as we could see data deleted as required but the session is failing.
Please let me know what is the issue here and also the solution.Thank you.

Fail to execute database migration: org.sonar.server.db.migrations.v44.ConvertProfileMeasuresMigration

RESOLVED: flushed sonar table.
When i go to http://localhost:9000/setup , after pressing "update" button , i get this error:
2014.08.06 04:08:03 ERROR [o.s.s.ui.JRubyFacade] Fail to upgrade database
An error has occurred, all later migrations canceled:
Fail to execute database migration: org.sonar.server.db.migrations.v44.ConvertProfileMeasuresMigration
Any suggestion please ?
Server: Centos 7
DB: Mysql
Mihai
You're facing the following issue: SONAR-5515. It will be fixed in the next version of SonarQube 4.5.
Meanwhile, you can run the SQL script given in the last comment of this ticket to fix your issue.

Sonar Installation problem

Information about Installation
Using embedded database Derbis
Problem :
I run bin/windows-x86-32/StartSonar.bat and then click http://localhost:9000
On clicking this, I get the following error:
We're sorry, but something went wrong.
Please try back in a few minutes and contact support if the problem persists.
<%= link_to "Go back to the homepage", home_path %>
Do you know what to do with that error message.
I usually get that message when the database isn't correctly set (either the local has an issue, in which case I try a fresh installation).
Or the mysql database you can associate has an issue (wrong 'sonar.jdbc.username' or 'sonar.jdbc.password' in sonar.properties file.).
Or the default port for the embedded database (jdbc:derby://localhost:1527/sonar;create=true) isn't available on your server/workstation.
If you are using a Mysql database (not your case), you need to create it first:
mysql [localhost] {root} ((none)) > CREATE DATABASE IF NOT EXISTS sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)
mysql [localhost] {root} ((none)) > grant all privileges on sonar.* to 'sonar'#'localhost' identified by 'sonar';
Query OK, 0 rows affected (0.00 sec)
mysql [localhost] {root} ((none)) > flush privileges;
Query OK, 0 rows affected (0.00 sec)
http://localhost:9000/setup will initialize the database and correct this error
Check the sonar log file for finding the issue you are facing. find out the error form the log file and try to solve it.
cheers..!
My OS is Mac.
If the version of sonar is 6.x, please try to run the follow steps:
Check the log: /Users/test/Downloads/sonarqube-6.0/logs/sonar.log
Whether it have the ERROR: “zh-CN” is not a valid locale or “en” is not a valid locale
Please run the command: gem uninstall -i /Users/test/.rvm/gems/ruby-2.3.0 i18n
The version of Ruby is your default ruby.

Resources