Is Singleton Class thread safe?
The singleton pattern isn't inherently thread-safe, but you can write thread-safe singletons.
Any class that has no writable class or instance data members is
inherently thread safe. Any method that uses only local variables or
parameters is inherently thread-safe. If a class has writable class or
data members, the sections of code that modifies them should appear in
a synchronized block.
If your singleton meets these criteria, I believe it's thread safe.
How do I create a thread-safe singleton class?
public class ExampleSingleton {
private static ExampleSingleton instance;
public static ExampleSingleton getInstance() {
if( instance == null ) {
synchronized( ExampleSingleton.class ) {
if ( instance == null ) {
instance = new ExampleSingleton();
}
}//sync ends
}
return instance;
}
}
Correct way of Integrating Hibernate Annotations with Spring. When it comes to design web application with database access, hibernate and spring are the first choice.
Spring for dependency injection and hibernate for persisting objects.
There are two ways of integrating Hibernate Annotations with Spring.
Using only applicationcontext.xml
Using applicationContext.xml and hibernate.cfg.xml file.
Method 1:
When we use only applicationContext.xml, we set all the properties of
sessionfactory in applicationContext.xml file and we use
AnnotationSessionFactoryBean class to create sessionFactory.
Eg:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">10000</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
This approach works fine only to some extent.
While using this approach we found that whenever you retrieve a small
object (say just 1 row) using session.get(object), the unnecessary
memory was getting used.
To retrieve a single row of object with two string fields was taking 5M of heap memory.
I was not having any idea where this memory is used but it was used.
Then we switched to other approach.
Method 2: (better method) In this method we define our applicationContext.xml and hibernate.cfg.xml file.
hibernate.cfg.xml file is defined as usual.
Now we refer this hibernate.cfg.xml file in applictaionContext.xml file.
Following is the code snippet from
applicationContext.xml file referring hibernate.cfg.xml file.
Eg:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" singleton="true">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
Only above configuration is required to refer hibernate.cfg.xml file in applicationContext.xml file.
Instead of referring hibernate.cfg.xml file in classpath, you can also give direct path of hibernate.cfg.xml file.
Hibernate.cfg.xml file should be defined in normal way as it is defined.
Eg:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@host:database </property>
<property name="hibernate.connection.username">uname</property>
<property name="hibernate.connection.password">pword</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="current_session_context_class">thread</property>
<mapping class="model.Employee"/>
</session-factory>
</hibernate-configuration>