Netbeans Entity Manager not updating Derby Database - java

I am currently using the automatically created class and Entity manager which is created when a table is bound to a database from NetBeans to get and set values to a derby database.
However when I want to update/edit the field using:
LessonTb Obj = new LessonTb();
Obj.setAdditionalResources(Paths);
Obj.setDescription(LessonDescription);
Obj.setLessonName(LessonName);
Obj.setLessonPath(LessonName + ".txt");
Obj.setRecommendedTest(RecommendedTest);
EUCLIDES_DBPUEntityManager.getTransaction().begin();
EUCLIDES_DBPUEntityManager.getTransaction().commit();
lessonTbList.clear();
lessonTbList.addAll(lessonTbQuery.getResultList());
The current Entry does not update in the database despite knowing that the code worked in other projects. I use the same get and set methods from the same LessonTb class which works to add a new entry and delete and entry.
What could possibly be wrong and how do I solve my problem? No exceptions are thrown.

Here's several possibilities. Perhaps you can do more research to rule at least some of them out:
You're using an in-memory database, and you didn't realize that all the database contents are lost when your application terminates.
You're not in auto-commit mode, and your application failed to issue a commit statement after making your update
You're not actually issuing the update statement that you think you're issuing. For some reason, your program flow is not reaching that code.
Your update statement has encountered an error, but it's not the sort of error that results in an exception. Instead, there's an error code returned, but no exception is thrown.
There are multiple copies of the database, or multiple copies of the schema within the database, and you're updating one copy of the database but querying a different one.
One powerful tool for helping you diagnose things more deeply is to learn how to use -Dderby.language.logStatementText=true and read in derby.log what actual SQL statements you're issuing, and what the results of those statements are. Here's a couple links to help you get started doing that: https://db.apache.org/derby/docs/10.4/tuning/rtunproper43517.html and http://apache-database.10148.n7.nabble.com/How-to-log-queries-in-Apache-Derby-td136818.html

Related

Liquibase SQL Rollback Statement doesn't work

So, I am currently trying to use Liquibase inside a java application. For now it works fine, I built myself a little tool, which creates and updates a h2 Database without any problems. The problem arises when I try to use formatted SQL changeSets.
I use a xml MasterChangelog. In this changelog I specify two changeLogs: One XML which contains the Database Structure and creates all needed tables for me, the second is a SQL changeLog containing all my insert statements with all the data.
updating works and the data is put into the database, problem I can't rollback. So I looked it up and learned that I need to put in a rollback statement for every changeset, which I did but it still doesn't work
Example: SOME_TABLE('ID_TABLE1', 'ID_TABLE2', 'someInput')
--changeset my.name:someId
INSERT INTO "PUBLIC"."SOME_TABLE" VALUES('someId', 'someOtherID', 'someInput');
--rollback DELETE FROM SOME_TABLE WHERE ID_TABLE1='someId'
this doesn't work
--changeset my.name:someId
INSERT INTO "PUBLIC"."SOME_TABLE" VALUES('someId', 'someOtherID', 'someInput');
INSERT INTO "PUBLIC"."SOME_TABLE" VALUES('someId', 'differentId', 'someInput');
INSERT INTO "PUBLIC"."SOME_TABLE" VALUES('someId', 'IdFromHell', 'someInput');
--rollback DELETE FROM SOME_TABLE WHERE ID_TABLE1='someId'
or this
I added a semicolon after the rollback, left them away after the inserts, making every insert its own changeset or grouping them together like above, but nothing works. I always get the infamous: "No inverse to liquibase.change.core.RawSQLChange created".
I tried these inside the h2 database and the statements for themselves work fine. I don't get what is wrong, whether using the console or my java logic it doesn't work. Does anybody know what's wrong?
If so, I am thankful for every hint I can get.

JPA using eclipselink and java.sql: when connect to DB

I explain better my question since from the title it could be not very clear, but I didn't find a way to summarize the problem in few work. Basically I have a web application whose DB have 5 tables. 3 of these are managed using JPA and eclipselink implementation. The other 2 tables are manager directly with SQL using the package java.sql. When I say "managed" I mean just that query, insertion, deletion and updates are performed in two different way.
Now the problem is that I have to monitor the response time of each call to the DB. In order to do this I have a library that use aspects and at runtime I can monitor the execution time of any code snippet. Now the question is, if I want to monitor the response time of a DB request (let's suppose the DB in remote, so the response time will include also network latency, but actually this is fine), what are in the two distinct case described above the instructions whose execution time has to be considered.
I make an example in order to be more clear.
Suppose tha case of using JPA and execute a DB update. I have the following code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit);
EntityManager em=emf.createEntityManager();
EntityToPersist e=new EntityToPersist();
em.persist(e);
Actually it is correct to suppose that only the em.persist(e) instruction connects and make a request to the DB?
The same for what concern using java.sql:
Connection c=dataSource.getConnection();
Statement statement = c.createStatement();
statement.executeUpdate(stm);
statement.close();
c.close();
In this case it is correct to suppose that only the statement.executeUpdate(stm) connect and make a request to the DB?
If it could be useful to know, actually the remote DBMS is mysql.
I try to search on the web, but it is a particular problem and I'm not sure about what to look for in order to find a solution without reading the JPA or java.sql full specification.
Please if you have any question or if there is something that is not clear from my description, don't hesitate to ask me.
Thank you a lot in advance.
In JPA (so also in EcliplseLink) you have to differentiate from SELECT queries (that do not need any transaction) and queries that change the data (DELETE, CREATE, UPDATE: all these need a transacion). When you select data, then it is enough the measure the time of Query.getResultList() (and calls alike). For the other operations (EntityManager.persist() or merge() or remove()) there is a mechanism of flushing, which basically forces the queue of queries (or a single query) from the cache to hit the database. The question is when is the EntityManager flushed: usually on transaction commit or when you call EntityManager.flush(). And here again another question: when is the transaction commit: and the answer is: it depends on your connection setup (if autocommit is true or not), but a very correct setup is with autocommit=false and when you begin and commit your transactions in your code.
When working with statement.executeUpdate(stm) it is enough to measure only such calls.
PS: usually you do not connect directly to any database, as that is done by a pool (even if you work with a DataSource), which simply gives you a already established connection, but that again depends on your setup.
PS2: for EclipseLink probably the most correct way would be to take a look in the source code in order to find when the internal flush is made and to measure that part.

Find out who deleted the row in the DB

We have a Oracle DB with around 4K tables in it. And around 30 different applications accessing the data. We have an issue where one of the application is deleting a row in one of our tables. We do not know which app and why. I'm trying to figure out, but, first thought I got is to use Triggers when ever something is deleted and log it, but, is there a way to find out which application has deleted it in oracle?
Thanks
If you didnt want to go down the autiting or logging route and the application is not distinct from v$session as it stands, you could set the name of the application by calling
dbms_application_info.set_client_info('my_application');
This sets v$session.client_info for the session, which you can read via
dbms_application_info.read_client_info(client_info out varchar2);.
You could then use triggers and to record this value.

Caching data on JDBC

I have created a database application using java swing. My program retrieves data from the database whenever I call the find class. The find class simply creates a statement, get the current database connection, then execute the statement. The returned values from the database will be placed on a ResultSet then will be displayed on a jTable. The problem is this:
I opened the find class, the result was displayed. Then I go to SQLyog or HeidiSQL (applications to manipulate the database), edit the values which was displayed on my program, then save. I went back to my program, close the find class then reopen it, I still get the previous data, not the edited one. Please help. Updates must be displayed once the find class is open. The only way for me to get the updated data is to close my entire program then reopen it, which I dont want to do.
EDIT:
This is what I tried. Basically, once my program creates a connection to the database the first time, I save it to another class which makes the connection always open(I assume). So whenever I want to create a query, I'll just call the class to get the connection. What I did now is, after executing the query from the find class, I close the connection with the .close() function. It works, but do I really need to do close the connection every time? Again this is just a desktop application, not a web program.
I think you're having a transaction isolation issue. Some drivers start an implicit transaction when it grabs the connection so you're seeing a snapshot of the database at that time. You probably want a READ COMMITTED level which should show the database with all committed transactions applied.
Looks like either you have not commit the edit or you need to refresh data by refreshing query.

function call problem

i am going to call a function which will retrieve some data value from database. but before that i am sending those data. i am just checking whether those data properly inserted or not with this function call. but inserting data taking some time to insert into the database but my function calling starts before it actually inserts the data into the database. As because of that it is finding that no data is inserted in the database. Can any one tell how do i resolve this issue. How to synchronize this. whether i should get the proper result after the proper insertion into the database. i cant use here runnable interface or thread class. the think that i have to do is to call the data access function after certain time so that data gets enough time to get inserted into the database. please help me out.
Don't know what language you are using, but maybe the function has a parameter which causes it to wait until the query finishes before returning? Something mentioning the word "synchronous?"
Use the Database Driver
I'm not familiar with JDBC so I'm not sure which tools are/aren't available to you, but it seems like you're doing more work than you need to.
Typically the database driver will inform you whether the query was executed successfully, so you should not need to have your application query the data afterward to verify that the data is there. Instead, ask the driver for errors to see whether there was a problem with the query.
If you're inserting a large about of data and your database supports it, you may want to use a transaction to perform your insert. This will pass all of the data into the db, attempt the insert, and warn you of any problems.
If there are problems with the transaction, you can roll back, and the database state will be the same as when you started (obviously you will need to handle the errors to save your data). If there are no problems, you can finish committing the transaction, and rest assured that the database state matches the application state.
Alternatives
If for some reason the above methods won't work, you can try to resolve the race condition using an event pattern. In simple terms, you want to raise an event when the data is done inserting to alert the validator that it can start reading data. The validator will listen for that event and trigger when it hears it.

Categories

Resources