Hi gnodet,
Iam attaching my complete example(most of the configuration i took from one of the threads
http://forum.springframework.org/showthread.php?t=32063)
Please see that iam using sessionFactory.getCurrentSession() as it is said in the documentation this is used to associate a session object with JTATransaction(If JTA is enabled)
My question is can i use session object like this across multiple threads, but it is said that session object is not thread safe(but here iam using JTATransaction) little bit confused.
It will be a great help for me if you look into my code and clarify. Thanks a lot in advance.
########## spring.xml ###########
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- XID factory -->
<bean id="xidFactory" class="org.apache.geronimo.transaction.manager.XidFactoryImpl" />
<!-- Transaction log -->
<bean id="transactionLog" class="org.jencks.factory.HowlLogFactoryBean">
<property name="logFileDir" value="./log/txlog"/>
<property name="xidFactory" ref="xidFactory"/>
</bean>
<!-- #### Transaction Manager #### -->
<bean id="transactionContextManager"
class="org.jencks.factory.TransactionContextManagerFactoryBean"
abstract="false" singleton="true"
lazy-init="default" autowire="default"
dependency-check="default">
<property name="transactionLog" ref="transactionLog"/>
</bean>
<bean id="userTransaction"
class="org.jencks.factory.GeronimoTransactionManagerFactoryBean"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default" />
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"
abstract="false" singleton="true" lazy-init="default" autowire="default"
dependency-check="default">
<property name="userTransaction" ref="userTransaction" />
</bean>
<!-- #### Connection Manager #### -->
<bean id="connectionTracker"
class="org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator"/>
<bean id="connectionManager" class="org.jencks.factory.ConnectionManagerFactoryBean">
<property name="transactionSupport">
<bean class="org.jencks.factory.XATransactionFactoryBean">
<property name="useTransactionCaching" value="true" />
<property name="useThreadCaching" value="true" />
</bean>
</property>
<property name="poolingSupport">
<bean class="org.jencks.factory.SinglePoolFactoryBean">
<property name="maxSize" value="2" />
<property name="minSize" value="1" />
<property name="blockingTimeoutMilliseconds" value="60" />
<property name="idleTimeoutMinutes" value="60" />
<property name="matchOne" value="true" />
<property name="matchAll" value="true" />
<property name="selectOneAssumeMatch" value="true" />
</bean>
</property>
<property name="connectionTracker" ref="connectionTracker"/>
</bean>
<!-- Datasource and Hibernate setup: -->
<bean id="ta20dbMCF"
class="org.jencks.tranql.XAPoolDataSourceMCF" lazy-init="true">
<property name="driverName" value="com.mysql.jdbc.Driver" />
<property name="user" value="root" />
<property name="password" value="" />
<property name="url" value="jdbc:mysql://localhost/test" />
</bean>
<bean id="ta20dbDataSource" lazy-init="true"
class="org.springframework.jca.support.LocalConnectionFactoryBean">
<property name="managedConnectionFactory">
<ref local="ta20dbMCF"/>
</property>
<property name="connectionManager">
<ref bean="connectionManager"/>
</property>
</bean>
<bean id="ta20dbHibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
</props>
</property>
</bean>
<bean id="ta20dbMappingResources" class="java.util.Vector">
<constructor-arg>
<list>
<value>com/epe/Honey.hbm.xml</value>
<value>com/epe/HoneyExtraInfo.hbm.xml</value>
</list>
</constructor-arg>
</bean>
<bean id="ta20dbSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="ta20dbDataSource"/>
</property>
<property name="hibernateProperties">
<ref bean="ta20dbHibernateProperties" />
</property>
<!-- OR mapping files. -->
<property name="mappingResources" ref="ta20dbMappingResources"/>
<property name="jtaTransactionManager">
<ref bean="userTransaction"/>
</property>
</bean>
<bean id="myEpe" class="com.epe.MyEPE">
<property name="sessionFactory" ref="ta20dbSessionFactory"/>
<property name="transactionManager" ref="txManager"/>
</bean>
</beans>
######## MyEPE.java #########
package com.epe;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
public class MyEPE{
private SessionFactory sessionFactory;
private PlatformTransactionManager transactionManager;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}
public void execute(){
Honey forestHoney = new Honey();
forestHoney.setName("forest honey");
forestHoney.setTaste("very sweet");
Honey countryHoney = new Honey();
countryHoney.setName("countryi honey");
countryHoney.setTaste("tasty");
DefaultTransactionDefinition definition=new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status=transactionManager.getTransaction(definition);
try{
Thread arr[]=new Thread[100];
for(int i=0;i<100;i++){
Honey obj = new Honey();
obj.setName("Honey"+i);
arr[i]= new AddRule(sessionFactory,obj);
}
for(int i=0;i<100;i++)
arr[i].start();
for(int i=0;i<100;i++)
arr[i].join();
Session tmp=((AddRule)arr[0]).getSession();
System.out.println("Conn Status before commit/rollback:"+tmp.isOpen());
transactionManager.commit(status);
System.out.println("Conn Status after commit/rollback:"+tmp.isOpen());
}catch(Exception e){
e.printStackTrace();
}
}
public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
}
########## BaseRule.java #########
package com.epe;
import org.hibernate.Session;
public abstract class BaseRule extends Thread {
Session session;
BaseRule(Session session){
this.session=session;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
}
######### AddRule.java ######
package com.epe;
import org.hibernate.SessionFactory;
public class AddRule extends BaseRule {
Honey honey;
AddRule(SessionFactory sessionFactory,Honey data){
super(sessionFactory.getCurrentSession());
this.honey=data;
}
public void run(){
try {
session.persist(honey);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
gnodet wrote:
I'm not sure what you are trying to do, but you can't expect a single
transaction to be splitted across different threads at the same time.
You can find some example configurations using jencks in the source
tree:
http://svn.jencks.codehaus.org/browse/jencks/trunk/jencks/src/test/resources/org/jencksOn 9/25/07, Arif Mohd <arif.mohammed1@wipro.com> wrote:
>
> Hi gnodet,
>
> It will be a great help for me if you could help me with small code
> snippet. Iam entirely new to this technologies and iam trying to evaluate
> how i can make use of jencks.
>
> As you said iam having a controller entity which will be creating new
> threads(hybernate sessions) parallely and i want to make use of jencks here
> in controller entity for maintaining distributed transactions.
>
> Just help me how can i do stand alone setup for jencks(with out any
> app server stuff) with resource adapters.
>
> gnodet wrote:
> >
> > Transactions usually occur within a single thread, even when
> > distributed. You'd have to sequence your processing somehow...
> >
> > On 9/25/07, Arif Mohd <arif.mohammed1@wipro.com> wrote:
> >>
> >> Hi,
> >>
> >> Iam new to this jencks and even to hybernate. Just i want to know can
> >> i
> >> integrate jencks with hybernate. My requirement is there are 3 threads, 2
> >> of
> >> them are hybernate session threads doing some DB operation and one more
> >> thread is using ActiveMQ. I want to make use of jenks for maintaing
> >> distributed transactions over these threads
> >> --
> >> View this message in context:
> >>
http://www.nabble.com/Does-jencks-canbe-integrated-with-hybernate--tf4514305.html#a12875825> >> Sent from the jencks - user mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list please visit:
> >>
> >>
http://xircles.codehaus.org/manage_email> >>
> >>
> >
> >
> > --
> > Cheers,
> > Guillaume Nodet
> > ------------------------
> > Blog:
http://gnodet.blogspot.com/> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >
http://xircles.codehaus.org/manage_email> >
> >
> >
>
> --
> View this message in context:
http://www.nabble.com/Does-jencks-canbe-integrated-with-hybernate--tf4514305.html#a12876311>
> Sent from the jencks - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>
http://xircles.codehaus.org/manage_email>
>
--
Cheers,
Guillaume Nodet
------------------------
Blog:
http://gnodet.blogspot.com/---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email