optimistic-lock | SpringBoot mybatis 乐观锁 代码示例 | Object-Relational Mapping library
kandi X-RAY | optimistic-lock Summary
kandi X-RAY | optimistic-lock Summary
SpringBoot mybatis 乐观锁 代码示例
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Transform transfer
- Update account
- Creates an OK result
- Bean compatible API
- Generate product info
- Convenience method for retrying
- Enable resource handlers
- Handle ApiException with error status
- Handler for ClassCastException
- Handle exception not supported
- Handle IO exception
- Handle null pointer exception
- Handle exception
- Handle exception handler
- Starts the Optimistic lock application
- Transformer
optimistic-lock Key Features
optimistic-lock Examples and Code Snippets
Community Discussions
Trending Discussions on optimistic-lock
QUESTION
I have PG table, where PK is populated by trigger. In pgAdmin it works well. NH mappings is:
...ANSWER
Answered 2022-Feb-04 at 08:35trigger-identity
generator is not supported by dynamic insert dynamic-insert="true"
. Related bug report: https://github.com/nhibernate/nhibernate-core/issues/1062
So just disable dynamic insert for your entity and it should work (dynamic-insert="false"
).
QUESTION
I'm implementing my first Web API with JSON responses and got confused handling concurrency control for optimistic locking. Right now I develop the server with ASP.NET Core 6 and the client is developed with Angular. The data store is a SQL database. In the future we want to open the API to third parties.
I see two options for handling optimistic locking:
A) ETag in header
B) version number in the body
With option A) I see those two problems:
- Since ETag is a header "Precondition Fails 412" needs to be send for failures. The error needs to be specified in the body, so the client can understand that it is a concurrency error.
For concurrency errors I would expect to send HTTP error code 409:
"...Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type." 2) When a collection/list of representations (e.g. all due orders) is returned from the server there is no possibility to send multiple ETags. The ETag in the header applies to the whole collection/list, but each single resource in the list can be individually modified by different users and needs to have a version number for concurrency detection. I don't see any other option than to send an individual version number/ETag as a property with each representation in the body.
I find it confusing when collections are treated differently than single resources. Since the client needs to store the ETags for each single resource it is natural to include the ETag as a property in its object model anyway.
Option B) avoids the issues of A): I'm free to send a 409 on failure and single resources and collections of resources are treated the same way. The problem is the DELETE verb.
There is a discussion about using a body in a DELETE request at [1]: Is an entity body allowed for an HTTP DELETE request?
The current version of the RFC states:
"A client SHOULD NOT generate a body in a DELETE request. A payload received in a DELETE request has no defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request."
What does "has no defined semantics" mean? What are the implications? I'm aware that some implementations are ignoring the body or have not implemented the option to send a body with DELETE. I still wonder if I could send a version number in the body. Generally, I don't understand the reasons a body should not be generated for DELETE? Why is it evil?
I understand that If-Match with ETag is the recommended solution to handle concurrency, but it can't handle basic collections. Why is there a 409 error code for concurrency issues, when the recommended solution ETag can't use it to be compliant? This is really confusing.
Keeping the version number in the body for concurrency detection would be consistent, but it does not work for DELETE.
Edited:
The above text was edited for clarification reflecting Everts comments.
I see the following options (even GET can be conditional I'm here only concerned about concurrency update issues):
A) Client requests use If-Match header. Server responses put ETag in header and in body
The ETag in the body gives the client a consistent way of handling single resources and collections. In case the collection needs to have a version on its own the ETag in the header is available and can also be used for conditional GET.
I have to give up sending 409 for concurrency errors.
B) Version number is sent in the body except for DELETE.
In the case of DELETE the version number must be either sent with a query parameter or with an If-Match header. It is not pleasant to have this exception, but a more clear 409 can be sent and single resources and collections are handled consistently.
I'm still unsure about what implementation to choose. Are there any clear criteria that can help with the decision? How has this problem been solved by others?
Do I misunderstand the usage of 409?
Update:
This German article https://www.seoagentur.de/seo-handbuch/lexikon/s/statuscode-412-precondition-failed/ explains the usage of 409 vs 412:
412 should only be used if the precondition (e.g. If-Match) caused the failure, while 409 should be used if the entity would cause a conflict. If a database with version id is used this id can be passed to the ETag and from the If-Match back to the version id. However, it is not forbidden to do it in the body of the entity itself. It just requires that the concept and how it works be explained, whereas the ETag solution just lets you refer to the HTTP specification. (comment: it still leaves the no body in DELETE problem)
The ETag with collections problem has more nuances:
a) If the returned collection is just data for a list view it normally doesn't hold all data of the displayed objects. If one item in the list is edited the client needs to first GET the full entity from a single resource URI and will get the required ETag with that request. It also provides are current version of the resource at the time of editing and not when the list was requested.
b) If the returned collection holds the full entity data and performance issues are relevant or many items independently need to be changed at once, than the ETags of each item can be passed to the client in the body.
Update 2:
The "Zalando RESTfull API and Event Guidelines" compare various options for optimisitc locking at [https://opensource.zalando.com/restful-api-guidelines/#optimistic-locking][2]
They favour sending the ETag in the body for server responses.
I currently think that this is the best option. The client than has the freedom to use that ETag or send a new Get on the single resource.
...ANSWER
Answered 2022-Jan-20 at 12:42To me "Conflict 409" is the right response for a concurrency error, but the RFC states that a "Precondition Fails 412" needs to be send, which is not clear enough. Of course an error description in the body can clarify the cause of the error, but I would rather send 409.
Use the HTTP status codes as they are defined, not what you think they should mean based on the name.
When a collection (e.g. all due orders) is returned from the server there is no possibility to send multiple ETags. Each single resource can be individually modified. Therefore the only option is to send the ETag in the body as a property of each representation in the JSON response. It is confusing that collections are treated differently than single resources. Also the client needs to include a property for the ETag in its object model in any case.
HTTP doesn't really have a concept of collections, but you can give the collection itself its own ETag as well.
I don't understand all the implications of the specific wording. What does "has no defined semantics" mean? What is the reasons a body should not be generated?
A delete request should only delete the resource located at the URI. If it's successful, we can assume that the URI that was used in the DELETE request will return a 404 or 410 after the request was successful.
If you want to conditionally parameterize deletions, delete multiple resources at once or delete something other than the resource specified in the URI, the DELETE
request is simply not appropriate for that use-case.
If you want to use ETags, just use an If-Match
header.
QUESTION
I'm new in spring and hibernate on this project I'm using netbeans v.8.2 and I'm using library spring 3.2.7 and hibernate 4.3.x what should I do to fix the error ?
The eror: Error creating bean with name 'dao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
this is the application context.xml
...ANSWER
Answered 2021-Oct-09 at 11:06You're missing the org.hibernate.cache.Provider
class on the classpath
.
org.hibernate.cache.Provider was removed in the change from Hibernate 3 to Hibernate 4.
You're using Hibernate 4, but the Hibernate-related classes from Spring you're using in applicationContext.xml
are from org.springframework.orm.hibernate3
package, so they need that missing dependency.
Try using the HibernateTemplate and LocalSessionFactoryBean from the org.springframework.orm.hibernate4
package.
If there isn't such a package in Spring 3.2.7, upgrade to Spring 4.
QUESTION
I am trying to write a HQL Query, which is similar to a MySQL Join. Below are my entities. As you can see below I am not using annotations in my Pojos. Instead I am using XML to do the mapping.
Stock
...ANSWER
Answered 2021-Aug-16 at 18:46You can use criteria implementation of hibernate and using alias you can join Below is some reference code that may help
QUESTION
ANSWER
Answered 2021-Jul-13 at 09:43I believe you overcomplicated your session factory creation, and mapping files are not being loaded.
Both of the following should work.
Approach 1:
QUESTION
I'm generating POJO from Derby 10.14.2.0 database in NetBeans IDE 11.3, Hibernate ORM 5.4.31.
It does not generate any relationship-mappings
in hbm.xml
or in entity classes like many-to-one/one-to-one
).
I used Derby sample project for mcve.
I've used two tables from Derby sample database.
Product and PurchaseOrder
Product table is referenced in PurchaseOrder.
Table PurchaseOrder
has FOREIGN_KEY_PRODUCT_ID.
can see in image below.
hibernate.cfg
...ANSWER
Answered 2021-Jun-13 at 10:11You were close to solving it. The short answer is: just add this line to your hibernate.cfg.xml
config file (add empty catalog):
QUESTION
My hbm configuration:
...ANSWER
Answered 2020-Dec-23 at 00:08I find one way - Just get all nested in cycle:
QUESTION
Now:
...ANSWER
Answered 2020-Nov-13 at 05:04Yes, i create triggers on all table in database.
QUESTION
I am trying to fire the following Hibernare quarry.
...ANSWER
Answered 2020-Sep-30 at 07:20Since you are using createSQLQuery method, the SQL Statement should be used. In this case, it should be
"select * from Rating as rating where rating.organization.idorganization = :idorganization"
Or use the HQL Method and appropriate HQL Query
QUESTION
I'm developing a simple Spring Boot CRUD application using JPA that manages products in a DB. This application behaves as expected in my testing in that products can be created, read, updated and deleted. Now I'm considering how this application will perform in a concurrent environment.
Reading https://www.baeldung.com/jpa-optimistic-locking which describes handling "multiple transactions in an effective and most importantly, error-proof way." But is this not handled by the Spring Boot framework? I've encountered CRUD apps in the past and never encountered locking being explicitly implemented. I assumed that any DB updates are thread-safe and locking is not required to be implemented explicitly but is this not always true?
Should I use locking to calculate for example product inventory? So if I have 3 products and 3 GET requests are invoked to read 1,2 and 3 product respectively there is a risk that one of the threads will access a product that the DB has not updated insufficient time to indicate that the product is no longer available as other threads have exhausted the inventory?
...ANSWER
Answered 2020-Sep-22 at 18:50Optimistic locking helps to prevent that you change something having old information of the entity.
Imagine the following scenario:
You have an Product entity and this product a purchase and a selling price.
Imagine one thread loads the enitity from the database and the purchase price is 10 dollar and the selling price is 12 dollar.
The code adjusts the selling price to 15 dollar but before this change is commited to the database another thread is also modifying the entity.
The other thread adjusted the purchase price and increased it to 20 dollar and this change is already committed to the database.
I'm pretty sure you do not want to set the selling price to 15 dollar if the purchase price is 20 dollar.
With optimistic locking the framework (i.e. hiberate) checks whether the entity that shall be saved is in the same state that it was when it has been loaded from the database. Otherwise it will throw a OptimisticLockingException. If the purchase price is still 10 dollar changing the selling price to 15 dollar is fine. But if the purchase price has changed somehow in the meantime the optimistic locking helps you to not set a new selling price on outdated information (in this case that the purchase price is 10 dollar)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install optimistic-lock
You can use optimistic-lock like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the optimistic-lock component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page