Archive for the ‘Spring’ Category
How to have nested @Transactional blocks with Spring 3 with common propagating ROLLBACK?
Wednesday, December 14th, 2011Problem:
Here’s the code
@Transactional
void outer(){
inner1(); // @Transactional
inner2(); // @Transactional
}
Both inner methods are transactional. Now, what I wanted to happen was, when outer() is called and if inner2() is rolled back, inner1() too should roll back.
Solution:
Use the NESTED option for the @Transactional annotation. As in the documentation, this is what happens when you do that,
Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.
And PROPAGATION_REQUIRED or REQUIRED option means,
Support a current transaction, create a new one if none exists.
Based on that the code changes as follows,
@Transactional(propagation = Propagation.NESTED)
void outer(){
inner1(); // @Transactional
inner2(); // @Transactional
}
You can read about the other options in Spring Doc. I found another decent explanation here.
How to configure Spring with Hibernate on Tomcat?
Sunday, November 21st, 2010The steps below guides how to set up the back end of a Java Webapp on Tomcat while using Spring with Hibernate as the ORM.
Configure Connection String
In the /WEB-INF/classes/conf/jdbc.properties, we store the connection info,
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/JTestjdbc.username=root jdbc.password=admin hib.dialect=org.hibernate.dialect.MySQLDialectConfigure Spring for Hibernate related beans: SessionFactory, Transaction Manager and HibernateTemplate
In the /WEB-INF/classes folder have the applicationContext.xml file.
<beans> <!-- ========================= HIBERNATE CONFIG ========================= --> <!-- Configurer that replaces ${...} placeholders with values from a properties file --> <!-- (in this case, JDBC-related settings for the dataSource definition below) --><bean id="propertyConfigurer"> <property name="location"><value>conf/jdbc.properties</value></property> </bean><!-- Local DataSource that works in any environment -->
<!-- Note that DriverManagerDataSource does not pool; it is not intended for production --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>${jdbc.driverClassName}</value></property><property name="url"><value>${jdbc.url}</value></property><property name="username"><value>${jdbc.username}</value></property><property name="password"><value>${jdbc.password}</value></property></bean><!-- Hibernate SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource"><ref local="dataSource"/></property><property name="mappingResources"><list><value>Product.hbm.xml</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hib.dialect}</prop><prop key="hibernate.show_sql">true</prop></props></property></bean><!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref local="sessionFactory"/></property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean></beans>HibernateTemplate is a Spring class to bridge between Hibernate. More details here
Configure the Persistence classes and DAO classes
In the <beans> tag of applicationContext, have the following
<!-- ========================= APP BEANS ========================= --><bean id="Product" class="gunith.jtest.model.impl.BasicProduct"></bean><bean id="ProductDao" class="gunith.jtest.dao.hibernate.ProductHibernateDao"><property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property></bean>Create the Mapping File
This is the definition of spring beans related to the business logic. BasicProduct is a persistence POJ class, the realization of an interface Product. The 2 are mapped in the /WEB-INF/classes/Product.hbm.xml. (Here we have another class called MeasurableProduct)
<hibernate-mapping><class name="gunith.jtest.model.Product" table="product"><id name="id"><generator class="native"></generator></id><discriminator column="type"></discriminator><property name="name"></property><property name="price"></property><many-to-one name="brand" class="gunith.jtest.model.Brand" column="brand_id" lazy="false"></many-to-one><subclass name="gunith.jtest.model.impl.BasicProduct" discriminator-value="0"></subclass><subclass name="gunith.jtest.model.impl.MeasurableProduct" discriminator-value="1"><property name="unit"></property></subclass></class></hibernate-mapping>Create The DAO
The Database operations of Product is done by the gunith.jtest.dao.hibernate.ProductHibernateDao. It extends org.springframework.orm.hibernate3.support.HibernateDaoSupport. (Note that there’s a setter for hibernateTemplate in spring config)
public abstract class ProductHibernateDao extends HibernateDaoSupport {/** Saves an object in the DB */ public Serializable add(Object obj) {return getHibernateTemplate().save(obj);}/** Gets all objects of a given class */ public List getAllObjects(Class entityClass) {List all = getHibernateTemplate().loadAll(entityClass);return all;}}
How to get rid of *access denied* Exceptions when deploying Spring On Tomcat?
Sunday, November 21st, 2010While I was deploying Spring, I got the following Exception,
java.security.AccessControlException: access denied (java.util.PropertyPermission cglib.debugLocation read)
The fix for such “access denied” is as follows,
- Open [CATALINA_HOME]/conf/policy.d/04webapps.policy (ON UBUNTU)
- Add the following to that file inside the grant{} block
permission java.util.PropertyPermission "cglib.debugLocation","read";
Likewise, for
access denied (java.lang.RuntimePermission getProtectionDomain)
Add this,
permission java.lang.RuntimePermission "getProtectionDomain";



















