Kill Oracle sessions

Some time the Oracle connections stay open, blocking the other transactions, there’s no other option than kill it.
We firstly need to identify it, as system user, execute the following query:

SELECT s.inst_id,
s.sid,
s.serial#,
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';

Check the connection you need to kill, and:

ALTER SYSTEM KILL SESSION 'sid,serial#'

Now, you have your licence to kill! 😀

Define the Java version in Maven

Directly in the pom.xml (resource definition is also included):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
            <excludes>
                <exclude>profiles/**</exclude>
            </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/profiles/${profile.name}</directory>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

Add datasource access in tests

In my web applications, the datasource is often defined in Tomcat (see here), when I execute my integration tests using JUnit, I don’t need Tomcat, but I need an access to the database, and to the Spring context too.
My solution is to create an abstract test which provides me these features, I use:

Doing this, there’s nothing to change in the class I test, I can check the database access, the transaction management or the bean definitions:

package net.classnotfound;
import java.sql.SQLException;
import javax.naming.NamingException;
import oracle.jdbc.pool.OracleDataSource;

import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/integrationTestContext.xml"})
public abstract class AbstractTest {
    
    private static final String PWD = "password";
    private static final String USER = "user";
    private static final String JDBC_URL = "jdbc:oracle:thin:@localhost:1521:classnotfound";
    private static final Logger LOG = LoggerFactory.getLogger(AbstractTest.class); 

    @BeforeClass
    public static void setUpClass() throws Exception {

        OracleDataSource ods = null;
        try {
            ods = new OracleDataSource();
        } catch (SQLException e) {
            LOG.error(null,e);
        }
        ods.setURL(JDBC_URL);
        ods.setUser(USER);
        ods.setPassword(PWD);
        LOG.info("DB connection, URL: {}; user: {}; pwd {}", new String[] {JDBC_URL, USER, PWD});

        SimpleNamingContextBuilder builder = null;
        try {
            builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
            builder.bind("java:comp/env/jdbc/applicationDatasource",ods);
        } catch (NamingException e) {
            LOG.error(null,e);
        }
    }
}

And to use it in my test, I just need to extend this class:

public class TestService extends AbstractTest{

    @Autowired
    private MyService myService;
...
}