Spring is a popular framework for developing Java™ applications. WebSphere® eXtreme Scale provides support to allow Spring to manage eXtreme Scale transactions and configure eXtreme Scale clients and servers.
<aop:aspectj-autoproxy/> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="ObjectGridManager" class="com.ibm.websphere.objectgrid.ObjectGridManagerFactory" factory-method="getObjectGridManager"/> <bean id="ObjectGrid" factory-bean="ObjectGridManager" factory-method="createObjectGrid"/> <bean id="transactionManager" class="com.ibm.websphere.objectgrid.spring.ObjectGridSpringFactory" factory-method="getLocalPlatformTransactionManager"/> </bean> <bean id="Service" class="com.ibm.websphere.objectgrid.spring.test.TestService"> <property name="txManager" ref+"transactionManager"/> </bean>This shows the transactionManager bean being declared and wired in to the Service bean that will use Spring transactions. We will demonstrate this using annotations and this is the reason for the tx:annotation clause at the beginning.
Session s = txManager.getSession();This returns the session for the POJO to use. Beans participating in the same transaction will receive the same session when they call this method. Spring will automatically handle begin for the Session and also automatically invoke commit or rollback when necessary. You can obtain an ObjectGrid EntityManager also by simply calling getEntityManager from the Session object.
@Transactional(propagation=Propagation.REQUIRED)
public class TestService implements ITestService
{
SpringLocalTxManager txManager;
public TestService()
{
}
public void initialize()
throws ObjectGridException
{
Session s = txManager.getSession();
ObjectMap m = s.getMap("TEST");
m.insert("Hello", "Billy");
}
public void update(String updatedValue)
throws ObjectGridException
{
Session s = txManager.getSession();
System.out.println("Update using " + s);
ObjectMap m = s.getMap("TEST");
String v = (String)m.get("Hello");
m.update("Hello", updatedValue);
}
public String query()
throws ObjectGridException
{
Session s = txManager.getSession();
System.out.println("Query using " + s);
ObjectMap m = s.getMap("TEST");
return (String)m.get("Hello");
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
public String queryNewTx()
throws ObjectGridException
{
Session s = txManager.getSession();
System.out.println("QueryTX using " + s);
ObjectMap m = s.getMap("TEST");
return (String)m.get("Hello");
}
public void testRequiresNew(ITestService bean)
throws ObjectGridException
{
update("1");
String txValue = bean.query();
if(!txValue.equals("1"))
{
System.out.println("Requires didnt work");
throw new IllegalStateException("requires didn't work");
}
String committedValue = bean.queryNewTx();
if(committedValue.equals("1"))
{
System.out.println("Requires new didnt work");
throw new IllegalStateException("requires new didn't work");
}
}
public SpringLocalTxManager getTxManager() {
return txManager;
}
public void setTxManager(SpringLocalTxManager txManager) {
this.txManager = txManager;
}
}
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]
{"applicationContext.xml"});
SpringLocalTxManager txManager = (SpringLocalTxManager)ctx.getBean("transactionManager");
txManager.setObjectGridForThread(og);
ITestService s = (ITestService)ctx.getBean("Service");
s.initialize();
assertEquals(s.query(), "Billy");
s.update("Bobby");
assertEquals(s.query(), "Bobby");
System.out.println("Requires new test");
s.testRequiresNew(s);
assertEquals(s.query(), "1");
Here we use a Spring
ApplicationContext. The ApplicationContext is used to obtain a reference
to the txManager and specify an ObjectGrid to use on this thread.
The code then obtains a reference to the service and invokes methods
on it. Each method call at this level causes Spring to create a Session
and do begin/commit calls around the method call. Any exceptions will
cause a rollback.void registerSpringBeanFactoryAdapter(String objectGridName, Object springBeanFactory)
"{spring}MyLoaderBean"
This would
cause ObjectGrid to ask Spring for a bean named "MyLoaderBean". This
approach can be used to specify Spring managed POJOs for any extension
point in ObjectGrid so long as the bean factory has been registered
up front. The ObjectGrid spring extensions are in the ogspring.jar
file. This jar file must be on the class path for spring support to
work due. If a JavaEE application running in an XD augmented ND then
the application should place the spring.jar file and its associated
files in the EAR modules. The ogspring.jar must also be placed in
the same location.
You can now subscribe to updates for the WebSphere eXtreme
Scale Information Center. This feed includes a description of the changes that occur in the monthly refreshes. Use the RSS feed to subscribe to changes using your favorite feed reader.