Oracle

Flushing caches for testing:

  • ALTER SYSTEM FLUSH BUFFER_CACHE;
  • alter system flush shared_pool;
Finding unusable, invalid, and disabled indexes:

  • select owner, index_name from dba_indexes where status='UNUSABLE';
  • select index_name, status from dba_indexes where status = 'DISABLED';
  • select owner,table_name,index_name,last_analyzed,status from dba_indexes where status != 'VALID';
Rebuild index:

  • alter index XXX.XXX rebuild;
Compute schema statistics:
  • exec DBMS_UTILITY.ANALYZE_SCHEMA('schemaName','COMPUTE');
Hints to pin/unpin data in buffer cache:

  • alter table XXX storage (buffer_pool keep);
  • alter index XXX storage (buffer_pool keep);
  • alter table XXX storage (buffer_pool default);
  • alter index XXX storage (buffer_pool default);
Recently executed queries:
  • SELECT a.USERNAME, b.sql_text, a.STATUS FROM V$SESSION a INNER JOIN V$SQLAREA b ON a.SQL_ID = b.SQL_ID;
  • select sql_text from v$sqlarea where sql_text like '%someText%' order by last_active_time desc;

SQL datapump importing/exporting




Oracle 11g R2 (11.2) sequence numbers skip 


Note on Using Sequences with Deferred Segments If you attempt to insert a sequence value into a table that uses deferred segment creation, the first value that the sequence returns will be skipped.


The solution is to run “alter system set deferred_segment_creation=false;” as system user. This setting is a one-time thing for a database install.

Out or disk space:

While running high volume and performance tests, Oracle keeps consuming all available disk space. Discovered it was the undo and temp tablespaces. The following statements recover the space.

ALTER DATABASE DATAFILE '/oracle_ee/app/oradata/orcl/undotbs01.dbf' RESIZE 200m;

ALTER TABLESPACE temp SHRINK SPACE KEEP 256M;

 

Moving temp and undo tablespaces:

select * from dba_tablespaces;
select * from dba_data_files;

CREATE TEMPORARY TABLESPACE temp2 TEMPFILE '/media/oradata/temp2_01.dbf'
SIZE 100M REUSE AUTOEXTEND ON MAXSIZE unlimited EXTENT MANAGEMENT LOCAL UNIFORM SIZE 64K;

ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2;

DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;

create undo tablespace undotbs2 datafile '/media/oradata/undo02.dbf' size 2000m AUTOEXTEND ON MAXSIZE unlimited EXTENT MANAGEMENT LOCAL;

alter system set undo_tablespace= undotbs2 ;

drop tablespace undotbs1 including contents and datafiles;

Oracle Partitioning

Oracle 11g R2 supports automatic partition management.

Mounting Red Hat LVM on Ubuntu

$ sudo apt-get install lvm2
$ sudo modprobe dm-mod

Scan your system for LVM volumes:
$ sudo vgscan

Activate the volume:
$ sudo vgchange -ay vg_troylt

Find the logical volume under LV column:
$ sudo lvs
Example:
LG VG ...
lv_root vg_troylt ...

Create a mount point for that volume:
$ sudo mkdir /mnt/rh_linux

Mount it:
$ sudo mount /dev/vg_troylt/lv_root /media/rh_linux -o ro,user

Oracle Unbreakable Linux (Red Hat Enterprise)

When installing, put the GRUB1 bootload onto the same partition or it will replace the Ubuntu GRUB2 bootloader.

When launching enter 'c' at grub menu to go into command line. Enter chainloader (hd0, and hit tab to list the partition options.

Select the Red Hat partition # (label it for easy identification) and close the command with )+1

chainloader (hd0, msdos4)+1
boot

The firewall is not open for port 1521.
Make sure to set ORACLE_HOME and ORACLE_HOME_LISTENER in oracle .bash_profile
Use dbstart and dbshut to start and shutdown the database
Have to edit /etc/hosts to associate the host name and IP address, have to update it when DHCP changes.
Have to edit tns listener config file to point to the correct hostname or IP or it won't start.


Customizing Grub2 menu:

Edit: /etc/grub.d/40_custom

Add:
menuentry "Redhat" {
insmod ext2
set root=(hd0,3)
linux /boot/vmlinuz-2.6.32-24-generic root=/dev/sda3 ro
initrd /boot/initrd.img-2.6.32-24-generic
}


Update grub2 menu:
sudo update-grub


Oracle

Disconnect sessions:


BEGIN
FOR c IN (
SELECT s.inst_id,
s.sid,
s.serial#,
p.spid,
s.username,
s.program,
s.machine,
s.process,
s.status,
s.schemaname
FROM gv$session s
JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE s.type != 'BACKGROUND' and s.USERNAME='FBCI5IT'
)
LOOP
EXECUTE IMMEDIATE 'alter system kill session ''' || c.sid || ',' || c.serial# || '''';
END LOOP;
END;

Remote JMX client



Add these properties to the target VM to specify the jmx port and allow remote connections so you can get to it from other systems without the need for authentication.

JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=9999"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.ssl=false"


The example below demonstrates the following:
  • Connection
  • Listing all MBeans
  • Listing all MBean attributes, types, and mutability
  • Listing all MBean operations, parameters, and return types
  • Getting an attributes current value
  • Setting an attributes value
  • Invoking operations

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.management.Attribute;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class MBeanClient
{
public static void main(String[] args) throws IOException, MalformedObjectNameException, NullPointerException,
InstanceNotFoundException, ReflectionException, IntrospectionException, AttributeNotFoundException,
MBeanException, InvalidAttributeValueException
{
// Connect to server
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

// List all MBeans
Set names = mbsc.queryNames(null, null);
for (Iterator i = names.iterator(); i.hasNext();)
{
ObjectName on = (ObjectName) i.next();
System.out.println("\tObjectName = " + on);
listAttributes(mbsc, on.getCanonicalName());
listOperations(mbsc, on.getCanonicalName());
}

// get value of SomeAttributeName
ObjectName someMBeanName = new ObjectName("TopNodeName:subNodeName=Foo,mbeanName=FooBar");
System.out.println("MaxFileSize: " + mbsc.getAttribute(someMBeanName, "SomeAttributeName"));

// set value of SomeAttributeName
mbsc.setAttribute(someMBeanName, new Attribute("SomeAttributeName", 10));

// get new value of SomeAttributeName
System.out.println("SomeAttributeName: " + mbsc.getAttribute(someMBeanName, "SomeAttributeName"));

// invoke operation
mbsc.invoke(someMBeanName, "someOperationName", null, null);
}

private static void listAttributes(MBeanServerConnection mbsc, String canonicalName)
throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException,
ReflectionException, IOException
{
// List all mbean attributes
ObjectName objectName = new ObjectName(canonicalName);
MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objectName);
MBeanAttributeInfo[] attrInfo = mbeanInfo.getAttributes();
for (MBeanAttributeInfo info: attrInfo)
{
System.out.println(info);
}
}

private static void listOperations(MBeanServerConnection mbsc, String canonicalName) throws MalformedObjectNameException, NullPointerException, InstanceNotFoundException, IntrospectionException, ReflectionException, IOException
{
ObjectName objectName = new ObjectName(canonicalName);
MBeanInfo mbeanInfo = mbsc.getMBeanInfo(objectName);
MBeanOperationInfo[] operInfo = mbeanInfo.getOperations();
for (MBeanOperationInfo info: operInfo)
{
System.out.println(info);
}
}
}


HOWTO install Flashplayer 11 on Firefox 8 on Ubuntu 10.04 64bit LTS


  • Download Flashplayer here

  • Copy libflashplayer.so to /usr/lib/firefox-8.0/plugins

  • Create symbolic link: sudo ln -s /usr/lib/firefox-8.0/plugins/libflashplayer.so /usr/lib/firefox-addons

  • Restart Firefox

Curl Rest Examples:

GET

curl -X GET -H "Content-Type:application/xml" "http://localhost:8080/some/path/505?username=foo&password=bar"

Create

curl -X PUT -H "Accept: *" -d @someFile.xml "http://localhost:8080/some/path?username=foo&password=bar"

Create or Update

curl -X POST -H "Content-Type:application/xml" -d @someFile.xml "http://localhost:8080/some/path/save?username=foo&password=bar"

Delete

curl -X DELETE -H 'Content-Type:application/xml' -d @someFile.xml "http://localhost:8080/some/path?username=foo&password=bar"

Upload file

curl -X PUT -H 'Content-Type:multipart/form-data' -F "someFile.csv=@someFile.csv;type=text/csv" "http://localhost:8080/some/path/505?username=foo&password=bar" --verbose

Download file

curl -H "Accept: *" "http://localhost:8080/some/path/506?username=foo&password=bar" --O "foo.bar" --verbose

Maven

Create html dependency report

mvn project-inf-reports:dependencies

Hibernate criteria aliases

If you create an alias, then create a criteria and join to another table, etc., it is only going to resolve {alias} to the root table that the alias was created for.


hibernateCriteria.createCriteria("orderItems", "i", Criteria.LEFT_JOIN ).add(
Restrictions.or(Restrictions.sqlRestriction("orderNo=?", orderNo, Hibernate.Long),
Restrictions.sqlRestriction("{alias}.item_id in (SELECT item_id FROM items WHERE item_id = ?)", itemId, Hibernate.LONG)
)
);


Lessons Learnt Blog:


Criteria c = session.createCriteria(BaseTable.class, "base");
Criteria secondCriteria = c.createCriteria("secondTable","second", CriteriaSpecification.LEFT_JOIN);
secondCriteria.add(Restrictions.sqlRestriction("{alias}.id = someCoolCondition");


From the Hibernate docs:

List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.createAlias("mate", "mt")
.add( Restrictions.eqProperty("kt.name", "mt.name") )
.list();



Multiple joins:

The following is a native sql template which has the replaced with a generated alias.


"EXISTS " + //
"(SELECT 1 " + //
" FROM" + //
" orderItems <aliasname>" + //
" WHERE" + //
" <aliasname>.orderId = {alias}.id" + //
" AND <aliasname>.name='<propertyname>'" + //
" AND <otherclause>" + //
")";

Linux rfcomm serial communication

Scan to discover Bluetooth device

Pair with Bluetooth device

Connect with SPP

In one shell/terminal - pipe the input from the Bluetooth device to a file

     cat /dev/rfcomm0 > datalog.txt

In another shell/terminal - echo some command to the Bluetooth device

     echo "8;" > /dev/rfcomm0

Quit the cat command

Inspect the output

     less datalog.txt

Windows Soft Link

mklink (a windows softlink)

Message Digest

MD5 throughput was ~15 GBytes/minute on a quad core Intel i5 @ 2.67GHz running Ubuntu Linux 10.04 and Java(TM) SE Runtime Environment (build 1.6.0_22-b04) Java HotSpot(TM) Server VM (build 17.1-b03, mixed mode)

The throughput was the same whether the digest was computed from bytes on disk or memory. org.apache.commons.codec.digest.DigestUtils org.apache.commons.codec.binary.Hex

From file
      String filename = "src/test/resources/somefile";
      is = new FileInputStream(filename);
      assertEquals("bd6b1dc1ac767b018003572a767d2d0d", DigestUtils.md5Hex(is));
Pipe filter stream
      is = new DigestInputStream( new BufferedInputStream(new FileInputStream(filename)), MessageDigest.getInstance("MD5"));

  /*
   * Implementations using DigestInputStream would not need the byte handling as it would be just one filter in a pipe
   * that is being pumped for some other purpose.
   */
      final byte[] buf = new byte[2048];
      while (dis.read(buf) == -1)
      {
      }

      final MessageDigest d = dis.getMessageDigest();
      assertEquals("bd6b1dc1ac767b018003572a767d2d0d", Hex.encodeHexString(d.digest()));

JVM thread dump

Java options, which lets you direct thread dumps generated via “kill -3” to a file:

-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=jvm.log

Pretty cool, but there’s one small catch: the thread dumps still get written to the JVM’s TTY as well as the file.