Unable to update database using SQLContainer!

Hi,

I’ ve little experience with using the SQLContainer, but am giving it a try!

I’m able to read data from my Oracle database. However updating fields in a record of the database is not successful!

I’ve the following very basic code (conpl is a JDBCConnectionPool initialized elsewhere):

OracleGenerator og = new OracleGenerator();
        
TableQuery tq = new TableQuery("mytable",conpl, og);
tq.setVersionColumn("OPTLOCK");

for (int i = 0; i < sqlCont.size(); i++) {
    Object id = sqlCont.getIdByIndex(i);
    RowItem item = (RowItem) sqlCont.getItem(id);
    item.getItemProperty("MYFIELD").setValue(new BigDecimal(2015));
    item.commit();
}

Besides that I’ve an Oracle database with one table called “mytable” with the field “MYFIELD” that I want to update. The table has additional fields like ID (primary key) and OPTLOCK!

The statement:

System.out.println("Demo: " + item.getItemProperty("MYFIELD").getValue()); yields the initial value of that field!

I understand from all the documentation that the commit should do the trick. But when viewing the database from within SQLDeveloper and doing a refresh, the field MYFIELD doesn’ t show the value 2015?

This I don’ t understand at all!

Am I misunderstanding some basic issue here?

Regards,
Gerard

Hi,

I can see at least one issue: You’re not calling commit() on the SQLContainer. SQLContainer works by default in buffered mode, so you need to call commit() in order to write the changes to the database.

-tepi

Hi,

Thanks for your reply!

Well, of course the following statement is positioned before the for-loop, that was missing from my first mail, sorry:

SQLContainer sqlCont = new SQLContainer(tq); Now I replaced the
item.commit()
within the for-loop by the following:

sqlCont.commit(); after the for-loop, yielding:

INFO: null which, is even more mysterious!

Regards,
Gerard

Hi,

sorry for not being that clear. What I meant is that you don’t need the item.commit() at all. SQLContainer should keep track of changes to the items. After your for-loop you should call sqlCont.commit() once.

If this doesn’t work there’s some bug somewhere. You might want to set the logging level to finest and look at the log for any SQL statements the SQLContainer is doing when you call commit.

-tepi

Hi,

Thanks again! I did remove the item.commit()!

Yes, that logging thing, where and how to set this in Eclipse?

Regards,
Gerard

Hi,

this depends on your environment. Please see
https://vaadin.com/book/-/page/advanced.logging.html
for some details.

If you’re using e.g. Tomcat it should be enough to create a logging.properties file to the root package of your web application and insert “.level=FINEST” into the file. After this the logging output from SQLContainer (including DB statements) will go to the server log (log target depends on server configuration).

-tepi

Hi,

Thanks, and I’ll try that tomorrow! Yes, I’m using Tomcat!

Regards,
Gerard

Hi,

Unfortunately, I’m not able to check the box in front of the option "
Enable Tomcat debug logging (not supported by this Tomcat version)
"!

What I find out that when I remove the setValue(…) then the commit doesn’t generate an error!?

Could it be an issue related to update privileges of the database itself?

Regards,
Gerard

You don’t need to check that box. Just add the logging.properties file to your project’s root package and look at Tomcat’s console output after that.

Is there some exception on the commit call? Posting that here could also help.

Hi,

By the way I don’ t get any exception only INFO: null!

Okay, created the logging.properties and added .level=FINEST to it! Restarted the server and ran the application.

But, where to look for the logging info?

Regards,
Gerard

You should look at the Tomcat console. If you start it from Eclipse this should be in the Console view (be sure to select the correct console if there’s more than one).

Hi,

Initially when I start up the Tomcat server I see logging info in the Console, but only then!

By the way I’m using Eclipse Luna with Tomcat 7.0.59 and Vaadin 7.2.0!

Regards,
Gerard

Hi,


Problem solved!

The issue was that initially my table contained null values for the timestamp in my OPTLOCK column!

I replaced those null values with some arbitrary timestamp. After that modification the update worked as expected!

However the clue of the OPTLOCK column is not clear to me, because the OPTLOCK value for the modified row in the table didn’t change accordingly! Why?

Regards,
Gerard