Atomikos Forum |
|
I am working with your download Atomikos TransactionsEssentials and am unable to get the UserTransactionImp to work properly.
Configuration: - J2SE/JPA application with Hibernate 5 - MySQL server/database Page 18 of the PDF document for TransactionsEssentials refers to: "...sample program in examples/j2se/simple/jdbc/xadatasource" I am not able to find this anywhere. Application: There are two test methods, x() and x2() listed below. They are both called from the main() method. private void x() { EntityManager em = emf.createEntityManager(); UserTransactionImp utx = new UserTransactionImp(); try { //begin a transaction utx.setTransactionTimeout(60); utx.begin(); //access the datasource and do any JDBC you like Connection conn = ds.getConnection(); Message message = new Message(); message.setText("Hello World!"); em.persist(message); //always close the connection for reuse in the //DataSource-internal connection pool conn.close(); utx.commit(); Message m = em.find(Message.class, 1L); System.out.println("message = " + m.getText()); [printout shows "Hello World!"] System.out.println("id = " + m.getId()); [printout shows "1"] } catch (Exception e) { try { utx.rollback(); e.printStackTrace(); } catch (IllegalStateException | SecurityException | SystemException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally { if (em != null && em.isOpen()) em.close(); } } private void x2() { EntityManager em = emf.createEntityManager(); UserTransactionImp utx = new UserTransactionImp(); try { //begin a transaction utx.setTransactionTimeout(60); utx.begin(); //access the datasource and do any JDBC you like Connection conn = ds.getConnection(); Message message = new Message(); message.setText("Hello World - again!"); em.persist(message); //always close the connection for reuse in the //DataSource-internal connection pool conn.close(); utx.commit(); List<Message> messages = em.createQuery("select m from Message m").getResultList(); ********[list is empty]*************************** for (Message m : messages) { System.out.println("id: " + m.getId()); System.out.println("message: " + m.getText() + "\n"); } } catch (Exception e) { try { utx.rollback(); e.printStackTrace(); } catch (IllegalStateException | SecurityException | SystemException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally { if (em != null && em.isOpen()) em.close(); } } This has been a very frustrating experience. I would be most grateful for your assistance. Thank you for your assistance.
Hi,
this example does miss some environment infos and what you want to achieve. Also its unclear how your hibernate persistence unit is configured - e.g. its unclear if you did configure the used JTAPlatform so that Hibernate is aware of Atomikos JTA TransactionManagment at all. You also need to configure your database for MySQL to use pinGlobalTxToPhysicalConnection. Next question is - did you configure your persistence unit to actually use JTA or is it just resource_local? So could you add some fully contained sample showing your problems please, would it make easier to help you. Just from the current code snippets it won't work and i can't even try to find a problem or suggest a solution. kind regards Torsten
Torsten,
Thanks for your reply, you raise some good points I was unable to see earlier. B/c of my inexperience in this area I’m not exactly sure what you need to know, so what follows may in the end prove unnecessary. But at this point I’d rather give you too much rather than not enough. Environment - Datebase: MySQL (v. 5.1.38) - Persistence unit: JPA 2.1 w/Hibernate as persistence provider. I’m to keep this as JPA generic as possible as avoid using any Hibernate/vendor features if possible. - transaction-type = RESOURCE_LOCAL. From your reply this is for sure NOT correct. Please advise as to the correct setting. I’ve little experience in this area and would be grateful if you could be explicit and pretend that I’m an idiot. - Other settings: <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <!-- Configure Connection Pool --> <property name="hibernate.c3p0.min_size" value="5" /> <property name="hibernate.c3p0.max_size" value="20" /> <property name="hibernate.c3p0.timeout" value="500" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.idle_test_period" value="2000" /> Please note the ‘hibernate’ reference, my understanding is that these are not ‘native’ to Hibernate per se. When I was writing the domain model classes and defining the mappings on which I rely to create the database(s) I needed a persistence unit that worked to create the database schema. What you see above comes from some material I found on the internet. It’s worked for writing the domain models but from your reply I have the feeling a lot of this is incorrect and/or unnecessary. - Server: none apart from MySQL Workbench that I use to check the database content and configuration. Because of its size and complexity (due to ‘business’ requirements) the application will very likely reside on a server once it is finished. At this stage, however, it needs to be developed on a ‘non-server’ basis .. AND .. in a way that will enable relatively adaptation in the future. Note: I am aware of the ‘container management’ advantages that come with having the application on a server. However, my experience in this area is very minimal, and while I’m not adverse to learning something new I simply can’t afford to spend much time on ‘server’ issues while other matters that only I can handle require just as much attention. For that reason the application needs to be developed on a non-server, JSE basis. If all works to plan, then I’ll be able to enlist someone who has forgotten more about servers than I will ever know, … and that’s perfectly fine. - Objective: a ‘transaction manager’ that works together with a JPA entity manager to commit to the database changes made in the application. Here’s a general pattern: … EntityManager em = JPAUtils.createEntity Manager(); UserTransaction tx = JPAUtils.createTransaction(); // this is where Atomikos comes in try { tx.begin(); //… [do JPA stuff] tx.commit(); } catch(Exception ex) { [handle exception] } finally { [close entity manager] } - Lastly, in trying to make this all work I encountered something call AtomikosDataSourceBean. I don’t quite understand how this fits in the picture. For sure I can see its role as a link/connection between the data source/database and the application (including the transaction manager and JPA features), but exactly how that works remains a mystery because I can’t see where the bean is linked to the transaction manager or anything else apart from the data source that it wraps. I mention the bean here so that when you reply you’ll know this isn’t completely new. Danke sehr fuer Ihre Antwort, und auch voraus fuer Ihre Geduld die sicher gebraucht warden wird durch Mein Schreiben durchzuschwimmen. Hoffentlich hilft es Ihnen, mir etwas sowohl verstaendlich als auch nutzlich weiterzugeben. But please do NOT give any technical answers in German :). Kind regards JR
Torsten,
Thanks for your reply, you raise some good points I was unable to see earlier. B/c of my inexperience in this area I’m not exactly sure what you need to know, so what follows may in the end prove unnecessary. But at this point I’d rather give you too much rather than not enough. Environment - Datebase: MySQL (v. 5.1.38) - Persistence unit: JPA 2.1 w/Hibernate as persistence provider. I’m to keep this as JPA generic as possible as avoid using any Hibernate/vendor features if possible. - transaction-type = RESOURCE_LOCAL. From your reply this is for sure NOT correct. Please advise as to the correct setting. I’ve little experience in this area and would be grateful if you could be explicit and pretend that I’m an idiot. - Other settings: <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> <!-- Configure Connection Pool --> <property name="hibernate.c3p0.min_size" value="5" /> <property name="hibernate.c3p0.max_size" value="20" /> <property name="hibernate.c3p0.timeout" value="500" /> <property name="hibernate.c3p0.max_statements" value="50" /> <property name="hibernate.c3p0.idle_test_period" value="2000" /> Please note the ‘hibernate’ reference, my understanding is that these are not ‘native’ to Hibernate per se. When I was writing the domain model classes and defining the mappings on which I rely to create the database(s) I needed a persistence unit that worked to create the database schema. What you see above comes from some material I found on the internet. It’s worked for writing the domain models but from your reply I have the feeling a lot of this is incorrect and/or unnecessary. - Server: none apart from MySQL Workbench that I use to check the database content and configuration. Because of its size and complexity (due to ‘business’ requirements) the application will very likely reside on a server once it is finished. At this stage, however, it needs to be developed on a ‘non-server’ basis .. AND .. in a way that will enable relatively adaptation in the future. Note: I am aware of the ‘container management’ advantages that come with having the application on a server. However, my experience in this area is very minimal, and while I’m not adverse to learning something new I simply can’t afford to spend much time on ‘server’ issues while other matters that only I can handle require just as much attention. For that reason the application needs to be developed on a non-server, JSE basis. If all works to plan, then I’ll be able to enlist someone who has forgotten more about servers than I will ever know, … and that’s perfectly fine. - Objective: a ‘transaction manager’ that works together with a JPA entity manager to commit to the database changes made in the application. Here’s a general pattern: … EntityManager em = JPAUtils.createEntity Manager(); UserTransaction tx = JPAUtils.createTransaction(); // this is where Atomikos comes in try { tx.begin(); //… [do JPA stuff] tx.commit(); } catch(Exception ex) { [handle exception] } finally { [close entity manager] } - Lastly, in trying to make this all work I encountered something call AtomikosDataSourceBean. I don’t quite understand how this fits in the picture. For sure I can see its role as a link/connection between the data source/database and the application (including the transaction manager and JPA features), but exactly how that works remains a mystery because I can’t see where the bean is linked to the transaction manager or anything else apart from the data source that it wraps. I mention the bean here so that when you reply you’ll know this isn’t completely new. Danke sehr fuer Ihre Antwort, und auch voraus fuer Ihre Geduld die sicher gebraucht warden wird durch Mein Schreiben durchzuschwimmen. Hoffentlich hilft es Ihnen, mir etwas sowohl verstaendlich als auch nutzlich weiterzugeben. But please do NOT give any technical answers in German :). Kind regards JR
Hello Torsten,
The transaction-type="RESOURCE_LOCAL" setting should probably remain; the MySQL database is on the desktop, I can't think of any other way to reference it. It seems that a 'jta-data-source' property can be set in the persistence.xml file. Would this have something to do with the problem, and if so, then how should it be configured? Thanks, JR
Hm do you actually have 2 XA-Resources participating in the transaction? If not resource_local is fine, otherwise you need to configure it to JTA to make it work.
I need some time to figure out your post and maybe hack up some small self contained (maven/spring) example which does help you hopefully - sorry for the delay in answering, come back 2u asap.
Torsten - thanks for getting back on this. What I'm trying to do is:
- Write a prototype JPA application using JavaFX for the UI. All the work is done on a desktop PC where everything resides including the database. I don't need JTA at this stage, getting everything to work with a single database is the main goal at this point. - The app is for businesses that manage trusts, private investment companies and other related vehicles. If you're at all familiar with money laundering and how it happens, then THIS is it. - A overall consideration I have in writing the app is to make sure that the future revisions (they're inevitable) can be done as seamlessly as possible without having to start over again. - It's possible - indeed likely - that LOCAL_RESOURCE is all that I'll need. JPA has a VERY basic built-in transaction manager that I've started to use just so that when it's replaced by a more robust solution the 'space' for the replacement is already there. Hope this helps, and thanks for your input. JR |