Showing posts with label oracle. Show all posts
Showing posts with label oracle. Show all posts

Monday, December 5, 2011

Runnig Jetty from Maven with JNDI Data Source

During the development of a java web component (aka war) it can be very useful to run the application as quick as possible. Jetty provides a Maven plugin that allows running it directly from maven build or explicitly using "mvn jetty:run" from the command line.

But what happens if the war uses external database? Especially when the datasource is defined externally to the war and accessed vie the JNDI?

Actually the solution if quite simple:

Step 1 - Define the Data-source

Create the file defining the data-source:
(The example below is for Oracle. See this page for examples of the other databases.
<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/my_ds</Arg>
        <Arg>
            <New class="oracle.jdbc.pool.OracleDataSource">
                <Set name="DriverType">thin</Set>
                <Set name="URL">jdbc:oracle:thin:@(DESCRIPTION=(ENABLE=BROKEN)(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=something_to_replace)))
                </Set>
                <Set name="User">my_user</Set>
                <Set name="Password">my_password</Set>
                <Set name="connectionCachingEnabled">true</Set>
                <Set name="connectionCacheProperties">
                    <New class="java.util.Properties">
                        <Call name="setProperty">
                            <Arg>MinLimit</Arg>
                            <Arg>10</Arg>
                        </Call>
                        <Call name="setProperty">
                            <Arg>InactivityTimeout</Arg>
                            <Arg>600</Arg>
                        </Call>
                    </New>
                </Set>
            </New>
        </Arg>
    </New>
</Configure>

Pay attention to "jdbc/my_ds". It's the datasource JNDI name. Make sure to put there the actual JNDI name used by your application.

Place this file under your maven project. In my opinion the best is to place this file under src/dev/resources, but basically it can be anywhere.

To continue the example, I'll name the file: jetty-ds-dev.xml

Step 2 - Define the Jetty Maven Plugin

<build>
...
        <plugins>
...
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty-version}</version>
                <configuration>
                    <jettyConfig>src/dev/resources/jetty-ds-dev.xml</jettyConfig>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc14</artifactId>
                        <version>${oracle-ojdbc-version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Pay attention to the configuration of jetty-ds-dev.xml.
Also pay attention that Jetty must be able to find the relevant JDBC driver in its classpath!

And basically that's all. Run "mvn jetty:run" and your application should work with the provided database.


Recommended Reading

1. Apache Maven 3 Cookbook
2. Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation

Monday, March 14, 2011

Oracle: How to find last query executed

One more sql script to remember - How to find the last query executed:
select sql_text from v$sql 
where first_load_time=(select max(first_load_time) from v$sql)

Or may be in a bit more useful way:
select * from v$sql order by first_load_time desc

And even more useful, show only queries that contain MY_TABLE:
select * from v$sql where sql_text like '%MY_TABLE%' order by first_load_time desc

Update: Pay attention that the solution is not quite correct if you use the prepared statements. For prepared statements there will be one row added to the table per statement's parsing. So if you reused the same statement (which is a good practice from performance point of view), you won't get a new record in a table.


Recommended Reading

1. Oracle Essentials: Oracle Database 11g
2. Oracle PL/SQL Programming: Covers Versions Through Oracle Database 11g
3. Oracle SQL By Example (4th Edition)

Thursday, February 24, 2011

DB Creation Scripts

Two scripts to remember:

MS-SQL Create Database

CREATE DATABASE ${dbName}
GO

DECLARE @size int
SELECT @size = size*8 
FROM ${dbName}..sysfiles WHERE name = N'${dbName}'

IF @size < 5000
BEGIN
ALTER DATABASE [${dbName}] MODIFY FILE(NAME=N'${dbName}', SIZE=5MB)
END
ALTER DATABASE [${dbName}] MODIFY FILE(NAME=N'${dbName}', FILEGROWTH=5MB)

SELECT @size = size*8 
FROM ${dbName}..sysfiles WHERE name = N'${dbName}_log'

IF @size < 10000
BEGIN
ALTER DATABASE [${dbName}] MODIFY FILE(NAME=N'${dbName}_log', SIZE=10MB)
END
ALTER DATABASE [${dbName}] MODIFY FILE(NAME=N'${dbName}_log', FILEGROWTH=50MB)
ALTER DATABASE [${dbName}] ADD FILEGROUP userdata001

DECLARE @FilePath nvarchar(250)
SELECT @FilePath = LEFT(filename, LEN(filename) - CHARINDEX(N'\', REVERSE(filename))) FROM master.dbo.sysdatabases WHERE name = N'${dbName}'
SET @FilePath = @FilePath+ '\${dbName}_data001.ndf'
EXEC ('ALTER DATABASE [${dbName}] ADD FILE (NAME = N''${dbName}_data001'',FILENAME = '''+@FilePath+''',SIZE = 200MB,FILEGROWTH = 50MB) TO FILEGROUP userdata001')
ALTER DATABASE [${dbName}] MODIFY FILEGROUP userdata001 DEFAULT

GO

Oracle - Create User

CREATE USER TEST_USER IDENTIFIED BY TEST_USER
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;

GRANT "CONNECT" TO TEST_USER;
GRANT CREATE SEQUENCE TO TEST_USER;
GRANT CREATE TABLE TO TEST_USER;
GRANT CREATE TRIGGER TO TEST_USER;
GRANT UNLIMITED TABLESPACE TO TEST_USER;
GRANT CREATE VIEW TO TEST_USER;
GRANT CREATE PROCEDURE TO TEST_USER;
ALTER USER TEST_USER DEFAULT ROLE ALL;