Tag Archives: oracle

java hibernate example

Step 1: Create a Maven Project and add below dependencies into pom file,
this pom file will install all the dependency that required to work with Mysql and herbernate

  <dependencies>
	<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
	<dependency>
	    <groupId>org.hibernate</groupId>
	    <artifactId>hibernate-core</artifactId>
	    <version>4.1.6.Final</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>
  </dependencies>

Step 2: Add/Install JBoss plugin using Eclipse, To do it go into this menu location below
Eclipse-> Help -> Eclipse Marketplace

Step 3: Create an hibernate.cfg.xml file into project directory
File name: hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">rootpassword</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dbtest</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- create/update table operation into db  -->
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

Step 4: Create an Entity class Alien.Java

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Alien {

	@Id
	private int aid;
	private String aname;
	private String color;

	public int getAid() {
		return aid;
	}
	public void setAid(int aid) {
		this.aid = aid;
	}
	public String getAname() {
		return aname;
	}
	public void setAname(String aname) {
		this.aname = aname;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}

Step 4: Add below code into your desire function


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * Database
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        Alien alien = new Alien();
        alien.setAid(101);
        alien.setAname("jafor");
        alien.setColor("Green");
        
        Configuration con = new Configuration().configure().addAnnotatedClass(Alien.class);
        
        ServiceRegistry registry = new ServiceRegistryBuilder()
        		.applySettings(con.getProperties())
        		.buildServiceRegistry();
        
        SessionFactory sf = con.buildSessionFactory(registry);
        
        Session session = sf.openSession();
        
        Transaction tx = session.beginTransaction();
        
        session.save(alien);
        
        tx.commit();
    }
}

Killing Oracle Sessions (ALTER SYSTEM KILL / DISCONNECT SESSION)

There are a number of ways to kill rogue sessions both within Oracle and externally.
If you use Oracle sql developer then follow steps 1 and 2 and for others please scroll down to see the alternative ways.

Step 1:

Steps 2:

—————————- Alternative Ways ———————-

 ALTER SYSTEM KILL SESSION 

The basic syntax for killing a session is shown below.

 SQL> ALTER SYSTEM KILL SESSION 'sid,serial#'; 

In a RAC environment, you optionally specify the INST_ID, shown when querying the GV$SESSION view. This allows you to kill a session on different RAC node.

 SQL> ALTER SYSTEM KILL SESSION 'sid,serial#,@inst_id'; 

The KILL SESSION command doesn’t actually kill the session. It merely asks the session to kill itself. In some situations, like waiting for a reply from a remote database or rolling back transactions, the session will not kill itself immediately and will wait for the current operation to complete. In these cases, the session will have a status of “marked for kill”. It will then be killed as soon as possible.

In addition to the syntax described above, you can add the IMMEDIATE clause.

 SQL> ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE; 

Identify the Session to be Killed
Killing sessions can be very destructive if you kill the wrong session, so be very careful when identifying the session to be killed. If you kill a session belonging to a background process you will cause an instance crash.

Identify the offending session using the [G]V$SESSION and [G]V$PROCESS views as follows.

 
SET LINESIZE 100
COLUMN spid FORMAT A10
COLUMN username FORMAT A10
COLUMN program FORMAT A45

SELECT s.inst_id,
       s.sid,
       s.serial#,
       --s.sql_id,
       p.spid,
       s.username,
       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';

   INST_ID        SID    SERIAL# SPID       USERNAME   PROGRAM
---------- ---------- ---------- ---------- ---------- ---------------------------------------------
         1         30         15 3859       TEST       sqlplus@oel5-11gr2.localdomain (TNS V1-V3)
         1         23        287 3834       SYS        sqlplus@oel5-11gr2.localdomain (TNS V1-V3)
         1         40        387 4663                  oracle@oel5-11gr2.localdomain (J000)
         1         38        125 4665                  oracle@oel5-11gr2.localdomain (J001)

SQL>

For More : Killing Oracle Sessions