spring-boot


How to persist/commit entity to database immediately in Activiti Service Task


I have a need to persist(insert) a entity to database immediately when the save (or saveAndFlush) code is called.
However although the entity is created in the context it is not persisted in the database immediately.
We are using Spring Boot.
public interface MessageRepository extends JpaRepository<MessageEntity, Long> {
}
In the Service class
#Service
public class TestService {
#Autowired
private MessageRepository messageRepository;
#Transactional
public MessageEntity saveMessage(MessageEntity entity) throws Exception {
entity = messageRepository.saveAndFlush(entity);
return entity;
}
}
Though the entity is created it is not persisted/committed to the database immediately.
We are facing this issue within the Activiti Tasks only.
Any feedback will be appreciated.
One way of overcoming this situation is by taking advantage of the REQUIRES_NEW transaction attribute.
In your situation you would have to create a new repository:
public interface MessageRepositoryCustom{
void saveAndFLush(MessageEntity ent);
}
public MessageRepositoryCustomImpl implements MessageRepositoryCustom{
#Autowired
private SessionFactory sessionFactory;
#Transactional(propagation = Propagation.REQUIRES_NEW)
void saveAndFLush(MessageEntity ent){
Session session = sessionFactory.getCurrentSession();
session.persist(ent);
}
}
Then in your service you would use that repository:
#Transactional
public MessageEntity saveMessage(MessageEntity entity) throws Exception {
entity = messageRepositoryCutom.saveAndFlush(entity);
// other processing
return entity;
}
}
Now after the messageRepositoryCutom.saveAndFlush method has finished processing the entity will be physically persisted in the database as it was created in a separate transaction which has been commited.
This worked.
#Component
public class MessageRepositoryCustomImpl implements MessageRepositoryCustom {
#PersistenceContext
EntityManager entityManager;
#Override
#Transactional(propagation = Propagation.REQUIRES_NEW)
public MessageEntity saveImmediate(MessageEntity entity) {
entityManager.persist(entity);
return entity;
}
}

Related Links

jhipster multiple databases
Access-Control-Allow-Origin header not found after CORS filter added
Thymeleaf and Spring MVC: PUT request leads to null-values in form while the same code works with POST request
Completely auto DB upgradable Spring boot application
spring cloud stream 'bindingService' error
Spring Cloud Exit Codes & docker restart
How can I use Angular2 PathLocationStrategy in a Spring Boot application?
How client and server mapping message on Spring boot websocket
Spring-boot ConditionalOnProperty with map-based properties
PropertiesLauncher fails to restart
Accessing the application properties in logback.xml
JPA: Select Rows Without Any Id
WebServlet failed to load in Springboot 1.3.1 with EmbeddedTomcat 8
FeignClient Invalid HTTP error with WAR-deployed SpringBoot application
Activemq+Spring+Weblogic XA transaction
Spring boot show all autoConfiguration

Categories

HOME
domain-driven-design
locale
nagios
where
crash
datetimepicker
esxi
symfony-sonata
localhost
jshell
collision-detection
country-codes
rethinkdb
ndepend
suitecrm
browsermob
exchange-transport-agents
openstack-horizon
versioning
simd
samba
gsuite
android-cursorloader
orgchart
setup-project
ds-5
blockly
dom4j
azure-vm-scale-set
ragel
var
nsdateformatter
office-interop
vertical-alignment
cakephp-3.2
ocpjp
react.rb
intermediate-language
cc
rmq
vertex-shader
huawei
toggleclass
mongodb-3.3
boost-range
instaparse
user-profile
window.location
clear
encode
play-json
transport
vanilla-forums
euro
jsonmodel
illegalargumentexception
breeze-sharp
zoneminder
dotcmis
nest-initiative
rails-migrations
banana-pi
internet-radio
mixradio
nativequery
extensibility
dynamic-expresso
removeall
dnsbl
django-tinymce
invalidoperationexception
report-viewer2010
event-propagation
dancer
nsscrollview
functional-specifications
chromebug
pitch-shifting
dongle
linq-to-mysql
cstring
stackless

Resources

Encrypt Message