spring-statemachine
                                
                                 spring-statemachine copied to clipboard
                                
                                    spring-statemachine copied to clipboard
                            
                            
                            
                        Which is a better approach to persist the spring state machine context or restore the state machine using the state enum on entity object?
I am new to spring state machine and trying to understand which is a better approach whether to persist the state context in a database or to restore the state machine from the domain object?
I am trying to implement state machine to handle an e-commerce Order fulfillment process. I have come across 2 different approaches to restore the state machine
Persist the state machine context as described here Restore from the domain object as described here I am not able to understand the pros and cons of each method and how to determine which is a better approach for processing large number of orders per day.
Behind a scenes all these kinda use same facilities. Original functionality were quite awkward to use(still present in code base) and there has been few iterations to make things much easier for a user. StateMachineContext tries to be overall solution which then allows relatively easy way to persist/restory and this is shown in datapersist sample.
Those other sample just use different ways to do things manually. Nothing wrong with it if it works. I just prefer what I did in datapersistas it give relatively easy config for jpa/mongo/redis and StateMachineService to hide much of the logic.
I can able to pull the code from git but unable to find the configuration for JPA in that. Can you please provide the details on that so that I can build it using Maven.
Also I need to configure the State Machine with mybatis for persistence of the states. I am not sure how Iam going to achieve this. Please guide me.
Currently jpa config is pretty much done via Spring Boot and relevant class enabling this is StateMachineJpaRepositoriesAutoConfiguration.
I've never used mybatis and literally don't know anything about it. It looks like they are maintaining their own boot integration.
Thanks for your instant reply.
I understood the auto-configuration of JPA repository. Also can you please share the list of dependencies required for building datapersist, so that I can construct the same using maven.
My Current Status: Eventhough I am new to gradle, I managed to build V2.0.0 release using gradle and get the jar for datapersist and run and understand the same. Now I need to build the same using Maven and configure MySql database along with StateMachineFactory for my Order Management System.
Please provide your suggestions.
I found the dependencies from gradle. Now it is working. Thanks for the support.
@ezrajohnson Little late but you can also find statemachine from https://start.spring.io which shows how spring libs are usually added to gradle/maven projects.
When using data persist approach using JpaStateMachineRepository with state machine factory combined using model factory configured with UmlStateMachineModelFactory, getting duplicate exception on the first event triggering.
Insert method is invoked twice like below. Do you have any suggestion to resolve this issue?
Hibernate: insert into state_machine (state, state_machine_context, machine_id) values (?, ?, ?) Hibernate: insert into state_machine (state, state_machine_context, machine_id) values (?, ?, ?)
Well that doesn't tell much. Maybe you can show more what you're doing.
Sorry for the inconvenience. I found the cause for the issue in my project.
I invoked the state machine from the Service method which is annotated as Transactional. So I got the above issue. After I move the state machine invocation outside the Transactional scope, then the issue is fixed.
Thanks for the support @jvalkeal
Hmm, that's weird as I've used machine within Transactional bean before. If you have time, feel free to post more code to show how you got that exception. Maybe there is an issue with some use cases.
Hi @jvalkeal, since this is an old post, I would like to know if there is now an easy way to save the state machine context. We are using a graph database and hence the persisters available for mongodb/redis/sql etc won't work.
Regards, Giriraj
@ezrajohnson The same happened to me
What do you say of my approach: https://github.com/rishiraj88/EmployeeOnboardingStateMachine/blob/main/EmployeeOnboarding/src/main/java/com/wm/ob/emp/config/EmployeeStateMachineConfig.java
I have used an Enum here. I may revisit my approach; you may clone my project code and openly suggest me nice ideas.