I have 2 REST apis where I "persist" data, in a "transaction". I'm curious to hear suggestions on how to ensure that if the 2nd one has trouble the first one could be "rolled back".
I currently have a retry loop and if that fails I send a delete to the first call.
It works fine, I was just curious if it is possible to use spring transactions to handle this.
For that matter, are there other options?
It would be very interesting and useful if something like this was possible, but I don't think it could be possible since there is no standard API for commits/rollbacks with REST APIs as there is with the JDBC API. With Spring, you could never simply mark a method as #Transactional since Spring would have no idea how to "roll back" anything you did over a REST call without having to explicitly state it.
Related
I have a java/jersey api that is called from the front end. I need to write tests for the java code. How the code is written is:
1. The api call executes the resource method, this calls a separate method that gets data from db and returns to the resource method. This then returns a javax.ws.rs.core.Response to the client.
This is going to be my first time writing tests, so please answer considering I know nothing. What is the best way to start here? And what types of tests should I write. Unit tests are what I’m aiming for here.
Now I have done a lot of research here and I’m leaning towards using JUnit + Mockito to do this. But how do I check for the data in a Response object?
And how should I check the other file that is getting data from db? I found out DBUnit that can do that, but do I need it?
Another framework I came across was Rest Assured. Do I need to include that also? Or can the same things be done with JUnit/Mockito?
I just want some direction from people who have tested out jersey api’s. And want to know what is the most common way to do this.
I do not think there is a best way to do this, what you need to test is often subjective and dependent on the context.
However, you can structure your code in a way that the most important is tested easily and what's left (integration) can be done later / with different tools.
What I suggest here is to follow the principles of the hexagonal architecture. The idea is to keep at the center of your application and without any kind of dependencies (imports ...) to any framework (jaxrs, jpa, etc.) all business rules. These rules can be easily designed with TDD. You will then have very short running tests. It may be necessary to use Mockito to mock implementations of SPI interfaces.
In a second time, you can use this "core" by wiring adapters to the outer world (HTTP, databases, AMQP, etc.), using API and implementing SPI interfaces.
If you want to test these adapters, you exit the scope of unit-tests, and write integration-tests. Integration with a framework, a protocol, anything really.
This kind of tests can use a wide variety of tools, from a framework-related mock (like Jersey test framework), in-memory database (like H2), to fully operational middleware instance using tools like testcontainers.
What is important to remember when writing integration-tests is they are slow in regards of unit-tests. In order to keep a feedback-loop as short as possible, you will want to limit the number of integration-tests to a minimum.
Hoping this will help you!
I have quite a simple problem. I am rewriting very old app which is using direct access to database through DAO objects. There is no business layer (the code is not mine and is quite anti-code), so connection.setAutoCommit(false) is used for starting the transactions everywhere in the code. I had to rewrite the project because of security reasons, so it does not use database connection but webservices and hibernate/jpa on the J2EE server side (before it was standalone app, now app+j2ee). Simple - I just moved the DAO/VO objects to the webservice server and rewrote sql to hql and DAO in client replaced with webservice client.
But what to do with transaction code? Normally one transaction one webservice call. So I need some mechanism (parameter in webservices?) that could help me to reference to the same hibernate transaction across multiple webservice calls. Is it completely bad approach and should I just move the transactions in server code?
I think you should use SessionBeans expose as JAX-RS services, and let them control the transactions.
If you need to have a transaction accross multiple webservice calls, just define a new webservice, also a EJB SessionBean that acts as a facade for the other calls.
I think is a bad practice to implement what you suggest (with referecing the same hibernate transaction), and I think it might not even be possible. Each WS call is a separate thread, at different moment in times, mixing transactions across threads is not a good practice.
I also think this is bad practice, because you usually build webservice methods that are coarse grained. So usually you are fine with one request per transaction.
I can understand your need but think about the downsides:
How will you do a rollback about several transactions? This will introduce data inconsistencies, if not possible.
If this is possible, your webservice won't be stateless anymore,
which is commonly considered bad practice.
This means, your API requests will be dependent on each other, so you have prerequisites for executing any of your request.
Have you tried to put your transaction within one request? This might help to re-structure and possibly enhance the code of your app.
I have to implement a number of activities (for example, update a user's profile, transfer points from one user to another, etc...), each of which can be composed by one or more logical steps (check if user has enough points, subtract points from the first user, check if the other user can receive then, credit these points to the second user) in a given order. I also need to implement some kind of "rollback" mechanism so that I can undo any previous steps if something goes wrong with step N (kind of what one usually foods in database transactions, except for the fact that a database may or may not be involved).
Are there any Java libraries which can help me with this? I've had a look at Drools but it seems overly complex. Also, I'm not sure it supports this kind of rollback mechanism. Any ideas?
The JTA specifications is a framework defining a standard behavior for java transactions.
A typical and well known use case is the simple database transaction, but JTA is far more generic. It's a framework to manage transaction over one or more transactional resource. A transactional resource can be a database of course, but it can also be a file, a messaging service, ...
If you have multiple transactionnal resources implied in one transaction, you must search for a JTA implementation supporting XA-Transactions.(and here is another interesting link about XA)
I don't say that this is a simple framework... but the problem you are facing is not simple at all.
If you've got to integrate with other services/providers through REST/SOAP/EJBs/etc. then I recommend looking at Apache Camel. Camel is an integration service that can integrate with pretty much every service or protocol out there. And, I believe, it supports rudimentary transactions. You can make a single service call and Camel will handle the routing and integration with whatever backend services you define. And, you can chain them. So, your flow would look like this:
Client makes call to service 'FOO'...
'FOO' is defined as a route that makes a REST call to '/bar', followed by an EJB call to 'MyService', followed by persisting the results to a SQL database, and then lastly calling a SOAP web service. The client then gets back the return value from this call, which can be whatever transformation or permutation of these calls you want. It's completely transparent to the client where the result came from.
I wonder if there is a java framework to journalize operations done on objects and then save them in database.
In fact, I'm working on an application where a particular object undergo many operations, each one is changing its logic (many conrols may be applicated on the object depending on user).
Now, I would like to trace controls or operations done on this object and store them in new tables serving just for statistics. I think that this could be implemented without modifying the whole exiting code of the application. I mean it could be seen as a vertical layer...
I have already seen the description of hibernate interceptors but I'm not sure that it could meet my needs
I would like also to precize that I'm working with spring core and hibernate..
Anyone has an idea about a java framework or an API meeting my need
thanks in advance..
I'm sure Hibernate Interceptors can be helpful for you. But, there is a little change that your entities might have to go through, because interceptors work when saving all the entities, you have to let the interceptor know that you are not interested in saving a few of them by adding custom annotations to them.
Other ways of doing is by using Spring AOP, you can log work without touching any of your code, but for this to happen, you need to be using spring in your environment already.
Other ways could be using traditional Servlet filters to do this.
There is a concept of Hibernate Event handlers, you may also look it up.
I want to close statements automatically.
I want to achieve it using the technology of the following packages:
Java 1.5
Spring framework 2.5
It seems not to close statements automatically in the default settings, though I think that the transaction manager of Spring automatically shuts statements.
I do not want to call close() of statement as much as possible in the method for maintainability.
Is there a method to ensure that the statement's closed?
The amount of coding increases if coming for all the methods to have to call close() of statement, and there is a problem that the possibility that the omission is generated goes out.
Moreover, I am making the framework. It wants to make the restriction as much as possible by such a reason and to make a little method.
Moreover, is there an official site or document that shows the reason when there is no closing method?
I'm Japanese, so please explain using simple statements.
I believe Spring will handle all this for you if you use the SimpleJdbcTemplate. I'd recommend it highly.
The Spring transaction manager is not closing the statement. That's a separate concern. After all, you'd still want the statement closed even if you didn't have an open transaction.
I would not write your own framework. I would bet that you'd be hard-pressed to improve on what Spring 2.5 is already giving you. Perhaps it's more a matter of training and understanding Spring's capabilities better.