Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
TransactionManager tm;
Transaction t_old = tm.getTransaction();
tm.suspend();
Transaction t_new = tm.begin();
@Inject MyMapper mapper;
@Transactional(Transactional.TxType.REQUIRES_NEW)
void doFoo() {
mapper.doAny();
}
TransactionManager tm;
DataSource ds;
Transaction t1 = tm.begin();
Connection conn = ds.openConnection();
....
conn.close();
t1.suspend();
Transaction t2 = tm.begin();
conn = ds.openConnection();
....
conn.close();
t1.commit();
t2.commit();
Декларативный сервис тем и полезен, что создается по требованию, когда из ServiceReferenсe требуем сам сервис. Как только сервис освобождается, с помощью ungetService, он сразу разрушается.
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
if(usesCount.get() > 0){
long usesCountNow = usesCount.decrementAndGet();
log.debug("== Uses count of {} service are {}", original.getClass(), usesCountNow);
}
}
/**
* Allows services to provide customized service objects in the OSGi
* environment.
*
* <p>
* When registering a service, a {@code ServiceFactory} object can be used
* instead of a service object, so that the bundle developer can gain control of
* the specific service object granted to a bundle that is using the service.
*
* <p>
* When this happens, the {@code BundleContext.getService(ServiceReference)}
* method calls the {@code ServiceFactory.getService} method to create a service
* object specifically for the requesting bundle. The service object returned by
* the {@code ServiceFactory} is cached by the Framework until the bundle
* releases its use of the service.
*
* <p>
* When the bundle's use count for the service is decremented to zero (including
* the bundle stopping or the service being unregistered), the
* {@code ServiceFactory.ungetService} method is called.
*
* <p>
* {@code ServiceFactory} objects are only used by the Framework and are not
* made available to other bundles in the OSGi environment. The Framework may
* concurrently call a {@code ServiceFactory}.
*
* @param <S> Type of Service
* @see BundleContext#getService(ServiceReference)
* @ThreadSafe
* @version $Id: 535776e702ec5ace54f577218ff8f7920741558b $
*/
public interface ServiceFactory<S> {
Задачи использования полученных данных можно придумать самому. И это будет уже другая история.
<bundle>mvn:com.google.guava/guava/15.0</bundle>
<bundle>wrap:mvn:com.oracle/ojdbc14/10.2.0.5</bundle>
MyBatis и OSGi