掲示板

portlet plugins - service builder - rollback

thumbnail
12年前 に Jonatan Oyola によって更新されました。

portlet plugins - service builder - rollback

Regular Member 投稿: 193 参加年月日: 07/06/06 最新の投稿
I am using Liferay 6.0.6, and I created a portlet plugin. Then I added some services.

For example:


<entity name="Alumno" table="pa_alumno" local-service="true" remote-service="false">
		<column name="alumnoId" type="long" primary="true" />
		<column name="documento" type="long" />
		<column name="userId" type="long" />
		<column name="folderId" type="long" />

		<finder name="User" return-type="Collection">
			<finder-column name="userId" />
		</finder>

		<reference package-path="com.liferay.counter" entity="Counter" />
		<reference package-path="com.liferay.portal" entity="User" />
</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.


       @Transactional(readOnly = false, rollbackFor = { AlumnoException.class }, propagation = Propagation.REQUIRED)
	public Alumno addAlumno(Long companyId, String nombre, String apellido,
			Long documento, String email, String sexo, Date fechaNacimiento,
			String lugarNacimiento, String nacionalidad, Long cursoId,
			Locale locale) throws AlumnoException {

		boolean autoPassword = true;
		String password = StringPool.BLANK;
		boolean autoScreenName = true;
		String screenName = StringPool.BLANK;
		Long facebookId = 0L;
		String openId = StringPool.BLANK;
		int prefixId = 0;
		int suffixId = 0;
		Boolean sexoMasculino = Validator.equals(sexo, "H");
		Calendar calendar = CalendarFactoryUtil.getCalendar();
		calendar.setTime(fechaNacimiento);
		Integer mesCumple = calendar.get(Calendar.MONTH);
		Integer diaCumple = calendar.get(Calendar.DATE);
		Integer anioCumple = calendar.get(Calendar.YEAR);
		long[] groupIds = null;
		long[] organizationIds = null;
		long[] roleIds = null;
		long[] userGroupIds = null;
		ServiceContext serviceContext = new ServiceContext();

		User user;
		try {
			user = userLocalService.addUser(0L, companyId, autoPassword,
					password, password, autoScreenName, screenName, email,
					facebookId, openId, locale, nombre, StringPool.BLANK,
					apellido, prefixId, suffixId, sexoMasculino, mesCumple,
					diaCumple, anioCumple, "Alumno", groupIds, organizationIds,
					roleIds, userGroupIds, false, serviceContext);
		} catch (Exception e) {
			if (_log.isDebugEnabled()) {
				_log.debug(e.getMessage(), e);
			}
			throw new AlumnoException(
					"El email ya es utilizado por otro usuario");
		}
             Alumno alumno;
		try {
			long alumnoId = counterLocalService.increment(Alumno.class
					.getName());
			alumno = alumnoPersistence.create(alumnoId);
			alumno.setDocumento(documento);
			alumnoPersistence.update(alumno, false);
		} catch (Exception e) {
			if (_log.isDebugEnabled()) {
				_log.debug(e.getMessage(), e);
			}
			throw new AlumnoException("No se pudo crear el alumno", e);
		}

		return alumno;
	}


is it possible to create a transaction?

Thanks on advances
11年前 に Federico Miralles によって更新されました。

RE: portlet plugins - service builder - rollback

New Member 投稿: 4 参加年月日: 12/09/20 最新の投稿
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:

public class MyEntityLocalServiceImpl extends MyEntityLocalServiceBaseImpl {

	public createMyEntity (...) {
		try {
			//Creating liferay user...
			User user = userPersistence.create(CounterLocalServiceUtil.increment(User.class.getName()));
			//Setting other attributes...
			userPersistence.update(user, false);
			
			//Creating my own entity...
			MyEntity myEntity = myEntityPersistence.create(CounterLocalServiceUtil.increment(MyEntity.class.getName()));
			myEntity.setUserId(user.getUserId());
			//Setting other attributes...
                        myEntityPersistence.update(myEntity, false);

			//Forcing exception
			throw new Exception("Testing if a transaction works as it's supposed to be");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	...
}

How did you manage to handle this?

TIA

Federico
11年前 に MICHAIL MOUDATSOS によって更新されました。

RE: portlet plugins - service builder - rollback

Regular Member 投稿: 110 参加年月日: 11/10/04 最新の投稿
I haven't tried it myself (transaction with portal and custom services) but this post may help you