user3505901 user3505901 - 4 months ago 8
Ruby Question

JRuby OJDBC, no error but insert doesn't show up?

I've been following the tutorial from Oracle, using their JDBC driver with an oracle database and JRuby. I have the JRuby connector class required, and I modified some of their code. I just want to do a simple insert statement and execute it from my script. I may have to do this 10+ thousand times once this is done.

I'm using an oracle database, 11g. CentOS 7.2 x64.

This is the code I have that is doing the work:

def insert_row
insert_stmt = nil
begin
conn = OracleConnection.create(@dbuser, @dbpass, @dburl)

insert_sql = <<-EOH
INSERT INTO TESTTABLE2(COLUMN1)
VALUES('JRUBY')
EOH
puts insert_sql
insert_stmt = conn.prepare_statement(insert_sql)
insert_stmt.execute

rescue
puts "\n** Error occured **\n"
puts "Failed executing Oracle demo from JRuby ", $!, "\n"
ensure
# We have to ensure everything is closed here.
if (!insert_stmt.nil?)
insert_stmt.close
end
end
end


The code seems to finish successfully, but doesn't actually do anything. I just copied and pasted the jdbc_connector.rb file and required it, I assumed that's how you're supposed to use that since it's just for posting to the db.

These are my settings:
init = CSVParse.new("SYSTEM", "Password", "jdbc:oracle:thin:@...:1521:XE")

Please help.

Answer

If you're using the sample code from the link you provided, then it has @conn.auto_commit = false somewhere in your code, and the example shows the use of conn.commit right after the insert_stmt.execute, so that's very likely what you need to do.

In short, add missing conn.commit call.