Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Thursday, October 02, 2014

Transaction management - Spring Framework

Local vs. Global Transactions
Local transactions are specific to a single transactional resource like a JDBC connection, whereas global transactions can span multiple transactional resources like transaction in a distributed system.

Local transaction management can be useful in a centralized computing environment where application components and resources are located at a single site, and transaction management only involves a local data manager running on a single machine. Local transactions are easier to be implemented.

Global transaction management is required in a distributed computing environment where all the resources are distributed across multiple systems. In such a case transaction management needs to be done both at local and global levels. A distributed or a global transaction is executed across multiple systems, and its execution requires coordination between the global transaction management system and all the local data managers of all the involved systems.

Programmatic transaction management: This means that you have manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.

Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

A transaction attribute may have one of the following values:
Required
RequiresNew
Mandatory
NotSupported
Supports
Never

Transaction Isolation level
DEFAULT This is the default isolation level.
READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
REPEATABLE_READ Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.

Spring.xml for hibernate
<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- Enable Annotation based Declarative Transaction Management -->
    <tx:annotation-driven proxy-target-class="true"
        transaction-manager="transactionManager" />

    <!-- Creating TransactionManager Bean, since JDBC we are creating of type
        DataSourceTransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
   
    <!-- MySQL DB DataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/TestDB" />
        <property name="username" value="pankaj" />
        <property name="password" value="pankaj123" />
    </bean>

    <bean id="customerDAO" class="com.journaldev.spring.jdbc.dao.CustomerDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="customerManager" class="com.journaldev.spring.jdbc.service.CustomerManagerImpl">
        <property name="customerDAO" ref="customerDAO"></property>
    </bean>

</beans>



import org.springframework.transaction.annotation.Transactional;

import com.journaldev.spring.jdbc.dao.CustomerDAO;
import com.journaldev.spring.jdbc.model.Customer;

public class CustomerManagerImpl implements CustomerManager {

    private CustomerDAO customerDAO;

    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    @Transactional
    public void createCustomer(Customer cust) {
        customerDAO.create(cust);
    }

}



Spring transactions client driver

package com.journaldev.spring.jdbc.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.journaldev.spring.jdbc.model.Address;
import com.journaldev.spring.jdbc.model.Customer;
import com.journaldev.spring.jdbc.service.CustomerManager;
import com.journaldev.spring.jdbc.service.CustomerManagerImpl;

public class TransactionManagerMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");

        CustomerManager customerManager = ctx.getBean("customerManager",
                CustomerManagerImpl.class);

        Customer cust = createDummyCustomer();
        customerManager.createCustomer(cust);

        ctx.close();
    }

    private static Customer createDummyCustomer() {
        Customer customer = new Customer();
        customer.setId(2);
        customer.setName("Pankaj");
        Address address = new Address();
        address.setId(2);
        address.setCountry("India");
        // setting value more than 20 chars, so that SQLException occurs
        address.setAddress("Albany Dr, San Jose, CA 95129");
        customer.setAddress(address);
        return customer;
    }

}

Annotation style transaction
To use the annotation style transaction management all you have to do is to add a 3 simple bean configuration in your xml file i.e:

<context:annotation-config/>: Tells Spring framework to read @Transactional annotation

<tx:annotation-driven/>: Automatically adds transaction support which eventually wraps your code in transaction scope
Initializing DataSourceTransactionManager bean


Example:
@Transactional
public class AnnotatedUserDao implements IUserDao {

Example for read only:
@Transactional(readOnly = true)
public User selectUser(int uid) {

Programmatic transactions in Spring
public void deleteUser(final int uid) {
 DefaultTransactionDefinition paramTransactionDefinition = new    DefaultTransactionDefinition();

  TransactionStatus status=platformTransactionManager.getTransaction(paramTransactionDefinition );
try{
  String delQuery = "delete from users where id = ?";
  jdbcTemplate.update(delQuery, new Object[]{uid});
  platformTransactionManager.commit(status);
}catch (Exception e) {
  platformTransactionManager.rollback(status);
}



Wednesday, October 01, 2014

Java Application Tuning

Tuning objectives

Tools used
Jprofiler
JRockit mission control
Wily introscope
Fluke
Splunk
Wireshark

testing - load runner, winruuner, junit, jmeter

Measurement
CPU (ideally should not cross more than 50-75%)
Memory
IO usage
Response time
Latency
Throughput

Application level tuning

Java language tuning
Using hashmaps vs hashtable, array vs vector,
Use StringBuffer to Concatenate Strings
Assign null to Variables That Are No Longer Needed
Declare Constants as static final
Avoid Finalizers - Adding finalizers to code makes the garbage collector more expensive and unpredictable
Synchronize Only When Necessary

EJB

JNDI

Application Server
Precompile jsps

OS
File Descriptors
set rlim_fd_max = 8192
Verify this hard limit by using the following command:
ulimit -a -H
Once the above hard limit is set, increase the value of this property explicitly (up to this limit)using the following command:
ulimit -n 8192
Verify this limit by using the following command:
ulimit –a

DISK IO settings

TCP/IP settings

Virtual memory settings

Run TOP command in Linux to check CPU usage.
Run VMSTAT, SAR, PRSTAT command to get more information on CPU, memory usage and possible blocking.
Enable the trace file before running your queries,then check the trace file using tkprof create output file.
According to explain plan check the elapsed time for each query,then tune them respectively.

What is the use of iostat/vmstat/netstat command in Linux?
Iostat – reports on terminal, disk and tape I/O activity.
Vmstat – reports on virtual memory statistics for processes, disk, tape and CPU activity.
Netstat – reports on the contents of network data structures.

Caching

JVM tuning/Garbage collection
best GC algorithm, especially for web server applications, is the Concurrent Mark Sweep GC
operation mode to server

-Xms, -Xmx
-XX:PermSize=256 m -XX:MaxPermSize=256m --> this option is not there in new HotSpot JVM
Log GC -Xloggc:$CATALINA_BASE/logs/gc.log -XX:+PrintGCDetails
-XX:+PrintGCDateStamps
HeapDump on Out of memory - -Xloggc:$CATALINA_BASE/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps

jvm - space ratios,
gc type – CMS (concurrent mark and sweep, parallel), G1 (this is new in HotSpot)

In the event CPU usage rate is high - If TPS is low but CPU usage rate is high, this is likely to result from inefficient implementation. In this case, you should find out the location of bottlenecks by using a profiler. You can analyze this by using jvisuavm, TPTP of Eclipse or JProbe.
     
JMS tuning
quota - space for jms messages at queue or jms server level. if quota limit is reached, exception is raised
send timeout - blocking producers when quota limits are reached
paging messages
message buffer size - amount of memory stored in memory before messages are paged out
message compression
expired messages

JDBC code tuning
Use prepared statements. Use parametrized SQL.
Tune the SQL to minimize the data returned (e.g. not 'SELECT *').
Minimize transaction conflicts (e.g. locked rows).
Use connection pooling.
Try to combine queries and batch updates.
Use stored procedures.
Cache data to avoid repeated queries.
Close resources (Connections, Statements, ResultSets) when finished with.
Select the fastest JDBC driver.

JDBC parameter tuning
Increasing Performance with the Statement Cache
Connection Testing Options for a Data Source - test frequency/
Minimized Connection Test Delay After Database Connectivity Loss
Minimized Connection Request Delay After Connection Test Failures
Minimized Connection Request Delays After Loss of DBMS Connectivity
Minimizing Connection Request Delay with Seconds to Trust an Idle Pool Connection
A leaked connection is a connection that was not properly returned to the connection pool in the data source. To automatically recover leaked connections, you can specify a value for Inactive Connection Timeout on the JDBC Data Source

Threadpool/work manager
Contention/locks
Minimize I/O
Parellelization/concurrency

Hibernate tuning
property name="show_sql"

Tip 1 - Reduce primary key generation overhead
Tip 2 - Use JDBC batch inserts/updates
Tip 3 - Periodically flush and clear the Hibernate session
entityManager.flush();
entityManager.clear();

Tip 4 - Reduce Hibernate dirty-checking overhead
@Transactional(readOnly=true)
public void someBusinessMethod() {
}

Tip 5 - Search for 'bad' query plans - full table scans and full cartesian joins
Tip 6 - Use the second-level and query caches
Define lazy loading as the preferred association loading strategy,
Set ReadOnly to "true" on Queries and Criteria, when objects returned will never be modified.

Cache – 1st level = Session, 2nd = Hibernate, 3rd = Query results cache

SQL Tuning
How to see execution plan for an SQL
SQL> set autotrace onSQL> select count(*) from table_name;
SQL> explain plan set statement_id = '111' for select * from tableName;SQL> select * from plan_table;

How to find out cpu time for an SQL?
SQL> TIMING START select_empSQL> SELECT * FROM employee ; SQL> TIMING SHOW select_emp timing for: select_emp real: 1760
use indexes, improve h/w, caching, tune it, hints,
use explain plan or sql analyzer to see the execution path
add hints
rewrite sql
replace sql with pl/sql

How would you improve a poor performing query?

How do you tune a sql statement?

understand row level lock and how it migrates to table level lock

soa performance tuning - dehydrate, db persistence
http://docs.oracle.com/cd/E25178_01/core.1111/e10108/bpel.htm

Oracle HTTP server tuning
http://docs.oracle.com/cd/E25178_01/core.1111/e10108/http.htm#ASPER99006

Monday, July 01, 2013

JAX-RS Spring JPA Hibernate Maven web application

1. Workspace directory structure
C:\workspace\FFWD_NO_EJB>tree
Folder PATH listing
Volume serial number is 1C03-D338
C:.
├───.settings
├───src
│   └───main
│       ├───java
│       │   └───com
│       │       └───persistent
│       │           ├───entity
│       │           │   └───util
│       │           ├───rest
│       │           └───service
│       ├───resources
│       │   └───META-INF
│       └───webapp
│           └───WEB-INF
└───target

2. Entity java class
package com.persistent.entity;


import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@XmlRootElement
@Table(name="AUTH_STAT")
public class Status implements Serializable {

    @Id
    @Column(name="AUTH_STAT_CD")
    private String statusCode;

    @Column(name = "AUTH_STAT_DSC")
    private String statusDescr;

    @Column(name = "AUTH_SEREVITY_ID")
    private Integer severity;

    @Column(name = "CREATE_TS")
    private Date createTimestamp;

    @Column(name = "CREATE_USER_ID")
    private String createUserId;

    @Column(name = "UPD_USER_ID")
    private String updUserId;

    @Column(name = "UPD_TS")
    private Date updTimestamp;


3. REST service Impl (create a service interface too)

ackage com.persistent.rest;


import java.util.List;
import java.util.StringTokenizer;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.persistent.entity.Status;
import com.persistent.service.StatusService;

// The Java class will be hosted at the URI path "/persons"

@Component
@Path("/status")
public class StatusResourceWs implements StatusResource {
    @Autowired
    StatusService statusService;
    private Logger log = Logger.getLogger(this.getClass());
    // The Java method will process HTTP GET requests

    @GET
    @Path("{code}")
    @Produces("application/xml")
    public Status getStatus(@PathParam("code") String code) {
        log.info("Hit getStatus");
        Status status = statusService.getByCode(code);
        log.info("Sending: \n\n" + status + "\n\n");
        return status;
    }


4. JPA impl class (Crete a interface too)
package com.persistent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.persistent.entity.Status;
import com.persistent.service.StatusService;


@Service("statusService")
public class StatusServiceJpa implements StatusService {
    private Logger log = Logger.getLogger(this.getClass());
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    /* (non-Javadoc)
     * @see com.persistent.service.jpa.StatusService#getById(int)
     */
    @Transactional(readOnly = true)
    public Status getByCode(String code) {
        return entityManager.find(Status.class, code);
    }
}



5. Persistence.xml definition in META-INF directory of war file
<persistence xmlns="http://java.sun.com/xml/ns/persistence"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">

    <persistence-unit name="status_persistence" transaction-type="RESOURCE_LOCAL">
        <class>com.persistent.entity.Status</class>
    </persistence-unit>

</persistence>


6. Spring application context file contents - goes into WEB-INF/classes

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
            ">
    <!--  Scan for both Jersey Rest Annotations a -->
    <context:component-scan
        base-package="com.persistent.rest,com.persistent.service,com.persistence.entity" />
    <context:annotation-config />
   
    <tx:annotation-driven />
   
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/BPPSDataSource" expected-type="javax.sql.DataSource"/>
   
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
        <property name="persistenceUnitName" value="status_persistence" />
        <property name="jpaProperties">
            <value>
                  hibernate.query.factory_class=org.hibernate.hql.classic.ClassicQueryTranslatorFactory
                hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect
                hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
                  hibernate.show_sql=true
                hibernate.default_schema=BFPS
            </value>
        </property>
    </bean>
   
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />
   
    <bean id="jpaAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
        p:database="ORACLE" p:showSql="true" />
</beans>



7. web.xml definition
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Jersey Spring Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Spring Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>



8. Maven pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.persistent</groupId>
    <artifactId>FFWD_NO_EJB</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>FFWD_NO_EJB</name>
    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Maven 2 Repository</name>
            <url>http://download.java.net/maven/2/</url>
            <!-- <layout>default</layout> -->
        </repository>
        <repository>
            <id>maven-repository.dev.java.net</id>
            <name>Java.net Maven 1 Repository (legacy)</name>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
        </repository>
    </repositories>
    <build>
        <finalName>FFWD_NO_EJB</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
            </plugin>
        </plugins>
    </build>
    <properties>
        <spring.version>2.5.5</spring.version>
        <hibernate.version>3.3.2.GA</hibernate.version>
        <commons-dbcp.version>1.2.2</commons-dbcp.version>
        <commons-logging.version>1.0.4</commons-logging.version>
        <jersey-version>1.0.2</jersey-version>
        <log4j.version>1.2.13</log4j.version>
    </properties>
    <dependencies>
        <!-- Adding in JPA With It's Requirements -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons-logging.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>${spring.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commons-dbcp.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!--Explicitly add -->
        <!-- change cglib-nodep Farrukh Najmi From net.java.dev.jersey.users Dec.
            10, 2007 -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.1_3</version>
        </dependency>
        <dependency>
            <groupId>geronimo-spec</groupId>
            <artifactId>geronimo-spec-jta</artifactId>
            <version>1.0.1B-rc4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
           
            <!-- <artifactId>hibernate</artifactId> <version>3.2.5.ga</version> Explicitly
                remove: See: http://blog.interface21.com/main/2007/06/11/asm-version-incompatibilities-using-spring-autowired-with-hibernate/ -->
            <exclusions>
           
                <exclusion>
                    <groupId>javax.transaction</groupId>
                    <artifactId>jta</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>asm</groupId>
                    <artifactId>asm-attrs</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>cglib</groupId>
                    <artifactId>cglib</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- JPA Additions end. -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
</project>



9. Access REST url at http://localhost:7001/FFWD/rest/status/S

Wednesday, March 13, 2013

Spring Hibernate Java Maven war example

1. Project structure

C:.
├───.settings
├───src
│   └───main
│       ├───java
│       │   └───com
│       │       └───xyz
│       │           └───persistence
│       ├───resources --> where Status.hbm.xml and web-application-config.xml are
│       └───webapp
│           ├───META-INF
│           └───WEB-INF
└───target

2.Status.java POJO code - This represents a row in the mapped table
public class Status implements Serializable {

    private static final long serialVersionUID = 1L;

    public Status() {
        super();
    }

    private String statusCode;

    private String statusDescr;

    private Integer severity;

    private Date createTimestamp;

    private String createUserId;

    private String updUserId;

    private Date updTimestamp;

3. StatusDAO.java interface code
public interface StatusDAO {
    void save(Status status);
    void update(Status status);
    void delete(Status status);
    Status findByStatusCode(String statusCode);
}

4. StatusDAOImpl.java code

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class StatusDAOImpl extends HibernateDaoSupport implements StatusDAO {

    public void save(Status status) {
        getHibernateTemplate().save( status );
    }

    public void update(Status status) {
        getHibernateTemplate().update(status);
        
    }

    public void delete(Status status) {
        getHibernateTemplate().delete(status);        
    }

    public Status findByStatusCode(String statusCode) {
        List list = getHibernateTemplate().find(
                "from Status where statusCode=?",statusCode
          );
    return (Status)list.get(0);
    }

}


5. TestDriver.java code for running CRUD test cases

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestDriver {
    
    private StatusDAO statusDAO;
    
    public static void main(String[] args) {
        TestDriver driver = new TestDriver();
        try {
            driver.create();
            driver.retrieve();
            driver.update();
            driver.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public TestDriver() {
        super();        
        ApplicationContext appContext = 
            new ClassPathXmlApplicationContext("/web-application-config.xml");
        statusDAO = (StatusDAO)appContext.getBean("statusDAO");
    }

    public void create() throws Exception {    
        
        Status status = createStatus();
        
        // create a new status in db
        createStatus(status);

        // retrieve a status from db
        Status retrievedStatus = statusDAO.findByStatusCode("RR");

        // delete a status
        deleteStatus(retrievedStatus);
    }

    public void retrieve() throws Exception {        
        create();
    }

    public void update() throws Exception {    
        Status status = createStatus();

        // create a new status in db
        createStatus(status);

        // retrieve a status
        Status retrievedStatus = statusDAO.findByStatusCode("RR");

        // update a status
        updateStatus();    

        // delete a status
        deleteStatus(retrievedStatus);
    }

    public void delete() throws Exception {        
        Status status = createStatus();

        createStatus(status);

        // retrieve a status
        Status retrievedStatus = statusDAO.findByStatusCode("RR");

        // update a status
        updateStatus();

        // delete a status
        deleteStatus(retrievedStatus);
    }

    private Status createStatus() {
        Status status = new Status();

        status.setStatusCode("RR");
        status.setCreateTimestamp(new Date());
        status.setCreateUserId("sssss");
        status.setSeverity(new Integer(22));
        status.setStatusDescr("testing-sss");
        status.setUpdTimestamp(new Date());
        status.setUpdUserId("sssss");
        return status;
    }

    private void createStatus(Status status) {
        statusDAO.save(status);
    }

    private void updateStatus() {
        Status retrievedStatus = statusDAO.findByStatusCode("RR");
        retrievedStatus.setStatusDescr("updated description");
        statusDAO.update(retrievedStatus);
    }

    private void deleteStatus(Status retrievedStatus) {
        statusDAO.delete(retrievedStatus);
    }

6. Status.hbm.xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.xyz.persistence">
    <class name="Status" table="AUTH_STAT" mutable="false">
        <id name="statusCode" column="AUTH_STAT_CD" type="string"/>

        <property name="statusDescr" column="AUTH_STAT_DSC" type="string"/>
        <property name="severity" column="AUTH_SEREVITY_ID" type="integer"/>
        <property name="createTimestamp" column="CREATE_TS" type="timestamp"/>
        <property name="createUserId" column="CREATE_USER_ID" type="string"/>
        <property name="updUserId" column="UPD_USER_ID" type="string"/>
        <property name="updTimestamp" column="UPD_TS" type="timestamp"/>
    </class>
</hibernate-mapping>

7. Spring bean configuration file - web-application-config.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url"
            value="jdbc:oracle:thin:@dbhost:20009:sid" />
        <property name="username" value="ddd" />
        <property name="password" value="ddd" />
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.default_schema">SchemaName</prop>
            </props>
        </property>

        <property name="mappingResources">
            <list>
                <value>/Status.hbm.xml</value>
            </list>
        </property>

    </bean>

    <!-- Status Data Access Object -->
    <bean id="statusDAO" class="com.xyz.persistence.StatusDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>


8. index.jsp code to call test driver
<?xml version="1.0" encoding="UTF-8" ?>

<%@ page import="com.xyz.persistence.TestDriver"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hibernate + Oracle 11g DB CRUD test page</title>
</head>

<body>
    <%
        try {                
            TestDriver driver = new TestDriver();
            driver.create();
            out.println("Create Status succeeded");
            out.println("<br>");
            
            driver.retrieve();
            out.println("Retrieve Status succeeded");
            out.println("<br>");
            
            driver.update();
            out.println("Update Status succeeded");
            out.println("<br>");
            
            driver.delete();
            out.println("Delete Status succeeded");
            out.println("<br>");
            
            out.println("All CRUD operations succeeded");
        } catch (Exception e) {
            out.println("An exception occurred: " + e.getMessage());
        }
    %>
</body>

</html>


9. maven po.xml dependencies
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>

Tuesday, March 12, 2013

Hibernate Java Maven war example

1. Project structure
C:\workspace\testWebApp>tree
Folder PATH listing
Volume serial number is BED9-803F
C:.
├───.settings
├───src
│   └───main
│       ├───java
│       │   └───com
│       │       └───xyz
│       │           └───persistence
│       ├───resources --copy *.hbm.xml and hibernate.cfg.xml here
│       └───webapp
│           ├───META-INF
│           └───WEB-INF
└───target

2. Create Status Java POJO class - omitted getters and setters
import java.io.Serializable;
import java.util.Date;

public class Status implements Serializable {

 public Status() {
  super();
 }

 private String statusCode;

 private String statusDescr;

 private Integer severity;

 private Date createTimestamp;

 private String createUserId;

 private String updUserId;

 private Date updTimestamp;

3. Create status.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.xyz.persistence">
 <class name="Status" table="AUTH_STAT" mutable="false">
  <id name="statusCode" column="AUTH_STAT_CD" type="string"/>

  <property name="statusDescr" column="AUTH_STAT_DSC" type="string"/>
  <property name="severity" column="AUTH_SEREVITY_ID" type="integer"/>
  <property name="createTimestamp" column="CREATE_TS" type="timestamp"/>
  <property name="createUserId" column="CREATE_USER_ID" type="string"/>
  <property name="updUserId" column="UPD_USER_ID" type="string"/>
  <property name="updTimestamp" column="UPD_TS" type="timestamp"/>
 </class>
</hibernate-mapping>

4. Create hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@dbhost.company.com:20009:sid</property>
        <property name="hibernate.connection.username">userName</property>
        <property name="hibernate.connection.password">password</property>
        <property name="show_sql">true</property>
        <property name="hibernate.default_schema">SID</property>
        <mapping resource="Status.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

5. Create a test driver class (DAO)
public class TestDriver {
 
 public static void main(String[] args) {
  TestDriver driver = new TestDriver();
  try {
   driver.create();
   driver.retrieve();
   driver.update();
   driver.delete();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public TestDriver() {
  super();
 }

 public void create() throws Exception { 
  Status status = createStatus();
  
  // create a new status in db
  Session session = createStatus(status);

  // retrieve a status from db
  Status retrievedStatus = (Status) session.get(Status.class, "RR");

  // delete a status
  deleteStatus(session, retrievedStatus);
  
  HibernateUtil.shutdown();
 }

 public void retrieve() throws Exception {  
  create();
 }

 public void update() throws Exception { 
  Status status = createStatus();

  // create a new status in db
  Session session = createStatus(status);

  // retrieve a status
  Status retrievedStatus = (Status) session.get(Status.class, "RR");

  // update a status
  updateStatus(session); 

  // delete a status
  deleteStatus(session, retrievedStatus);
  
  HibernateUtil.shutdown();
 }

 public void delete() throws Exception {  
  Status status = createStatus();

  Session session = createStatus(status);

  // retrieve a status
  Status retrievedStatus = (Status) session.get(Status.class, "RR");

  // update a status
  updateStatus(session);

  // delete a status
  deleteStatus(session, retrievedStatus);
  
  HibernateUtil.shutdown();
 }
 
 public void runCRUDTestCases() throws Exception {
 }

 private Status createStatus() {
  Status status = new Status();

  status.setStatusCode("RR");
  status.setCreateTimestamp(new Date());
  status.setCreateUserId("sss");
  status.setSeverity(new Integer(22));
  status.setStatusDescr("testing-ssss");
  status.setUpdTimestamp(new Date());
  status.setUpdUserId("ssss");
  return status;
 }

 private Session createStatus(Status status) {
  Session session = HibernateUtil.getSessionFactory().openSession();
  session.beginTransaction();
  session.save(status);
  session.getTransaction().commit();
  return session;
 }

 private void updateStatus(Session session) {
  Status retrievedStatus;
  retrievedStatus = (Status) session.get(Status.class, "RR");
  retrievedStatus.setStatusDescr("updated description");
  session.beginTransaction();
  session.update(retrievedStatus);
  session.getTransaction().commit();
 }

 private void deleteStatus(Session session, Status retrievedStatus) {
  session.beginTransaction();
  session.delete(retrievedStatus);
  session.getTransaction().commit();
 }

6. HibernateUtil to create/close sessions
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

 private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
  try {
   // Create the SessionFactory from hibernate.cfg.xml
   return new Configuration().configure().buildSessionFactory();
  } catch (Throwable ex) {
   // Make sure you log the exception, as it might be swallowed
   System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
  }
 }

 public static SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 public static void shutdown() {
  // Close caches and connection pools
  getSessionFactory().close();
 }
}

7. Create index.jsp that calls test driver
<?xml version="1.0" encoding="UTF-8" ?>

<%@ page import="com.xyz.persistence.TestDriver"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hibernate + Oracle 11g DB CRUD test page</title>
</head>

<body>
 <%
  try {
   TestDriver driver = new TestDriver();
   driver.runCRUDTestCases();
   driver.create();
   out.println("Create Status succeeded");
   out.println("<br>");
   
   driver.retrieve();
   out.println("Retrieve Status succeeded");
   out.println("<br>");
   
   driver.update();
   out.println("Update Status succeeded");
   out.println("<br>");
   
   driver.delete();
   out.println("Delete Status succeeded");
   out.println("<br>");
   
   out.println("All CRUD operations succeeded");
  } catch (Exception e) {
   out.println("An exception occurred: " + e.getMessage());
  }
 %>
</body>

</html>

8. Copy Oracle DB driver class to application server or package it within war file for this work

9. pom.xml
<artifactId>testWebApp</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>testWebApp Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <build>
  <finalName>testWebApp</finalName>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
      <encoding>UTF-8</encoding>
      <source>1.4</source>
      <target>1.4</target>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <version>2.5</version>
     <configuration>
      <encoding>UTF-8</encoding>
     </configuration>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-eclipse-plugin</artifactId>
     <version>2.8</version>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>

 <!-- Project dependencies -->
 <dependencies>

  <dependency>
   <groupId>jstl</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.0</version>
  </dependency>
  
        <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.6.1</version>
  </dependency>
  
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>3.3.2.GA</version>
     </dependency>

  <dependency>
   <groupId>javassist</groupId>
   <artifactId>javassist</artifactId>
   <version>3.12.1.GA</version>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.0</version>
   <scope>test</scope>
  </dependency>

 </dependencies>