Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jonatan Oyola
portlet plugins - service builder - rollback
May 18, 2011 7:30 AM
Answer

Jonatan Oyola

Rank: Regular Member

Posts: 185

Join Date: June 6, 2007

Recent Posts

I am using Liferay 6.0.6, and I created a portlet plugin. Then I added some services.

For example:

 1
 2<entity name="Alumno" table="pa_alumno" local-service="true" remote-service="false">
 3        <column name="alumnoId" type="long" primary="true" />
 4        <column name="documento" type="long" />
 5        <column name="userId" type="long" />
 6        <column name="folderId" type="long" />
 7
 8        <finder name="User" return-type="Collection">
 9            <finder-column name="userId" />
10        </finder>
11
12        <reference package-path="com.liferay.counter" entity="Counter" />
13        <reference package-path="com.liferay.portal" entity="User" />
14</entity>


I created the method addAlumno in AlumnoLocalServiceImpl. I use the userLocalService for create a new user.

I need to do rollback if an error ocurrs in the method. The new created user must delete in case of error.

 1
 2       @Transactional(readOnly = false, rollbackFor = { AlumnoException.class }, propagation = Propagation.REQUIRED)
 3    public Alumno addAlumno(Long companyId, String nombre, String apellido,
 4            Long documento, String email, String sexo, Date fechaNacimiento,
 5            String lugarNacimiento, String nacionalidad, Long cursoId,
 6            Locale locale) throws AlumnoException {
 7
 8        boolean autoPassword = true;
 9        String password = StringPool.BLANK;
10        boolean autoScreenName = true;
11        String screenName = StringPool.BLANK;
12        Long facebookId = 0L;
13        String openId = StringPool.BLANK;
14        int prefixId = 0;
15        int suffixId = 0;
16        Boolean sexoMasculino = Validator.equals(sexo, "H");
17        Calendar calendar = CalendarFactoryUtil.getCalendar();
18        calendar.setTime(fechaNacimiento);
19        Integer mesCumple = calendar.get(Calendar.MONTH);
20        Integer diaCumple = calendar.get(Calendar.DATE);
21        Integer anioCumple = calendar.get(Calendar.YEAR);
22        long[] groupIds = null;
23        long[] organizationIds = null;
24        long[] roleIds = null;
25        long[] userGroupIds = null;
26        ServiceContext serviceContext = new ServiceContext();
27
28        User user;
29        try {
30            user = userLocalService.addUser(0L, companyId, autoPassword,
31                    password, password, autoScreenName, screenName, email,
32                    facebookId, openId, locale, nombre, StringPool.BLANK,
33                    apellido, prefixId, suffixId, sexoMasculino, mesCumple,
34                    diaCumple, anioCumple, "Alumno", groupIds, organizationIds,
35                    roleIds, userGroupIds, false, serviceContext);
36        } catch (Exception e) {
37            if (_log.isDebugEnabled()) {
38                _log.debug(e.getMessage(), e);
39            }
40            throw new AlumnoException(
41                    "El email ya es utilizado por otro usuario");
42        }
43             Alumno alumno;
44        try {
45            long alumnoId = counterLocalService.increment(Alumno.class
46                    .getName());
47            alumno = alumnoPersistence.create(alumnoId);
48            alumno.setDocumento(documento);
49            alumnoPersistence.update(alumno, false);
50        } catch (Exception e) {
51            if (_log.isDebugEnabled()) {
52                _log.debug(e.getMessage(), e);
53            }
54            throw new AlumnoException("No se pudo crear el alumno", e);
55        }
56
57        return alumno;
58    }


is it possible to create a transaction?

Thanks on advances
Federico Miralles
RE: portlet plugins - service builder - rollback
March 6, 2013 9:13 AM
Answer

Federico Miralles

Rank: New Member

Posts: 7

Join Date: September 20, 2012

Recent Posts

Hello Jonatan,

I'm facing the very same problem. I have to create a Liferay user and another custom entity related to that user. The thing is that if something goes wrong the whole transaction should be rolled back and none the instances should be saved. However using the following approach they do, I mean both of the instances are store in the database:
 1
 2public class MyEntityLocalServiceImpl extends MyEntityLocalServiceBaseImpl {
 3
 4    public createMyEntity (...) {
 5        try {
 6            //Creating liferay user...
 7            User user = userPersistence.create(CounterLocalServiceUtil.increment(User.class.getName()));
 8            //Setting other attributes...
 9            userPersistence.update(user, false);
10            
11            //Creating my own entity...
12            MyEntity myEntity = myEntityPersistence.create(CounterLocalServiceUtil.increment(MyEntity.class.getName()));
13            myEntity.setUserId(user.getUserId());
14            //Setting other attributes...
15                        myEntityPersistence.update(myEntity, false);
16
17            //Forcing exception
18            throw new Exception("Testing if a transaction works as it's supposed to be");
19        } catch (Exception ex) {
20            ex.printStackTrace();
21        }
22    }
23    ...
24}

How did you manage to handle this?

TIA

Federico
MICHAIL MOUDATSOS
RE: portlet plugins - service builder - rollback
March 12, 2013 4:09 AM
Answer

MICHAIL MOUDATSOS

Rank: Regular Member

Posts: 110

Join Date: October 4, 2011

Recent Posts

I haven't tried it myself (transaction with portal and custom services) but this post may help you
Federico Miralles
RE: portlet plugins - service builder - rollback
March 12, 2013 10:42 AM
Answer

Federico Miralles

Rank: New Member

Posts: 7

Join Date: September 20, 2012

Recent Posts

MICHAIL MOUDATSOS:
I haven't tried it myself (transaction with portal and custom services) but this post may help you


Hello Michail,

Thanks for you suggestion but I was able to solved it. As matter of fact I wrote a short blog post about it:

Liferay Transactional Custom Services Made Easy

Federico Miralles @Rotterdam CS BV