<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>By Programmer For Programmer &#187; Code Snippets</title>
	<atom:link href="http://byprogrammerforprogrammer.com/category/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://byprogrammerforprogrammer.com</link>
	<description>Here I lay down the useful tips, tricks and utilities for programmers like myself.</description>
	<lastBuildDate>Thu, 15 Sep 2011 19:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Script to check MySQL long running transactions</title>
		<link>http://byprogrammerforprogrammer.com/2011/02/script-to-check-mysql-long-running-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=script-to-check-mysql-long-running-transactions</link>
		<comments>http://byprogrammerforprogrammer.com/2011/02/script-to-check-mysql-long-running-transactions/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 17:51:29 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=95</guid>
		<description><![CDATA[We had an issue at work recently where our MySQL transactions weren&#8217;t being properly rolled back or committed, and we ended up with transactions that would last several hours, locking tables, until we killed them manually. After fixing the true issue, I wrote this script to check MySQL InnoDB status every 15 minutes (with cron) [...]]]></description>
			<content:encoded><![CDATA[<p>We had an issue at work recently where our MySQL transactions weren&#8217;t being properly rolled back or committed, and we ended up with transactions that would last several hours, locking tables, until we killed them manually.  After fixing the true issue, I wrote this script to check MySQL InnoDB status every 15 minutes (with cron) and email us if there are any transactions lasting longer than 15 seconds.</p>
<p>For testing, you can also pass in as the first argument a file name that contains some example &#8220;show innodb status&#8221; output to make sure it works.</p>
<pre>#!/usr/bin/sh
DB_USER=root
DB_PASS=password
STATUS_FILE=/tmp/innodb_status.out
TMP_FILE=/tmp/long_tx_check.out
MAIL_TO="email1@asdf.com;email2@asdf.com"
TX_THRESH=15

if [ -z $1 ]
then
	mysql -u $DB_USER -p$DB_PASS -A -e "show innodb status\G" &gt; $STATUS_FILE
else
	STATUS_FILE=$1
fi

cat $STATUS_FILE | awk "/TRANSACTIONS/,/FILE I/ { t=1 } /ACTIVE/ { if (\$5 &gt; $TX_THRESH &amp;&amp; t == 1) { on=1 } else { on=0 } } /thread id/ { if (on==1) { print \$0 } }" &gt; $TMP_FILE

FIRSTLINE=""
read -r FIRSTLINE &lt; "$TMP_FILE"
if [ "$FIRSTLINE" != "" ]
then
	echo "LONG RUNNING TRANSACTION FOUND... SENDING EMAIL"
	mail -s "LONG RUNNING TRANSACTION!!" "$MAIL_TO" &lt; $TMP_FILE
else
	echo "No long running transactions found"
fi</pre>
<p>This code was based off code taken from <a href="http://www.facebook.com/note.php?note_id=172915805932">here</a>.  Always give credit where credit is due <img src='http://byprogrammerforprogrammer.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2011/02/script-to-check-mysql-long-running-transactions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hibernate: Get Table Name from Mapped Class</title>
		<link>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hibernate-get-table-name-from-mapped-class</link>
		<comments>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 20:44:05 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=29</guid>
		<description><![CDATA[Given a class mapped in Hibernate, use this code to get the table name.]]></description>
			<content:encoded><![CDATA[<p>Given a class that is mapped in Hibernate, you can get the table name that it is mapped to with the following piece of code.</p>
<pre>
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.Joinable;

public String getTableName(SessionFactory sessionFactory, Class&lt;?&gt; mappedClass)
{
    ClassMetadata cmd = sessionFactory.getClassMetadata(mappedClass);

    //check that the class is mapped to something with a table name
    if (cmd == null || !Joinable.class.isInstance(cmd))
        return null;

    return Joinable.class.cast(cmd).getTableName();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/10/hibernate-get-table-name-from-mapped-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SupoSE Bash Update Script (cron)</title>
		<link>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=supose-bash-update-script-cron</link>
		<comments>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 03:54:52 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[supose]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=24</guid>
		<description><![CDATA[Script that can be used to update a Subversion index created with SupoSE, designed to be run via cron.]]></description>
			<content:encoded><![CDATA[<p>Hey everyone,</p>
<p>I created a bash script to update an index created by SupoSE, keeping track of what revision was last successfully updated.  I liked this better than their built-in scheduler because it has to continuously be running, and if for some reason our servers had to be restarted, it wouldn&#8217;t automatically start up again.  Thus, I made a script to set up in cron to run hourly.</p>
<p>To use this script:</p>
<ul>
<li>Create an index normally with SupoSE</li>
<li>Create a file to hold the last successfully indexed revision, and put this number in the file as the only text</li>
<li>Set the variables in the script below for your environment</li>
<li>Use bash 3.0 or higher, since I&#8217;m using regular expressions.  If this isn&#8217;t an option, you may just need to rewrite the parsing of &#8220;svn info&#8221; to get the HEAD revision.</li>
</ul>
<p>Enjoy!</p>
<pre>
#!/bin/sh

# Variables
SUPOSE_EXE="supose"
SUPOSE_LOC="/usr/local/supose/bin"
SVN_INDEX="/var/lib/supose/index/myindex"
SVN_URL="http://subversionurl/repo"
SVN_USER="username"
SVN_PASS="password"
LASTREV_FILE="/var/lib/supose/index/last.rev"

# Get the last successfully indexed revision
LASTREV=""
read -r LASTREV &lt; "$LASTREV_FILE"
if [ "$LASTREV" = "" ]; then
        echo "Please specify the last successfully indexed revision in $LASTREV_FILE"
        exit 1;
fi

# Get the "to" revision
TOREV=""
SVNINFO_CMD="svn info $SVN_URL"
SVNINFO=`$SVNINFO_CMD`
if [[ "$SVNINFO" =~ 'Revision: ([0-9]+)' ]]; then
        TOREV=${BASH_REMATCH[1]}
fi
if [ "$TOREV" = "" ]; then
        echo "**** ERROR ****"
        echo "Could not read the HEAD revision number from command: $SVNINFO_CMD"
        echo "Has subversion been updated with a different format?"
        echo "Output from command:"
        echo $SVNINFO
        exit 1;
fi

# Print some output
echo "Updating subversion: $SVN_URL"
echo "Last successful revision: $LASTREV"
echo "HEAD revision: $TOREV"
echo "Index location: $SVN_INDEX"
echo "..."

if [ "$LASTREV" -ge "$TOREV" ]; then
        echo "No revisions to update... skipping process"
        exit 0;
fi

# Add one to to get the FROM revision
let FROMREV=$LASTREV+1
if [ "$FROMREV" = "" ]; then
        echo "Error adding one to last successfully indexed revision: $LASTREV"
        exit 1;
fi

# Run the SupoSE command
SUPOSE_CMD="./$SUPOSE_EXE scan \
                --url $SVN_URL \
                --username $SVN_USER \
                --password $SVN_PASS \
                --fromrev $FROMREV \
                --torev $TOREV \
                --index $SVN_INDEX"
pushd $SUPOSE_LOC
echo "Running $SUPOSE_CMD"
$SUPOSE_CMD
popd

# Was there an error running SupoSE?
SUPOSE_STATUS="$?"
if [ "$SUPOSE_STATUS" -gt "0" ]; then
        echo "**** Error ****"
        echo "$SUPOSE_EXE returned status code: $SUPOSE_STATUS (not successful)"
        echo "File $LASTREV_FILE will not be updated with latest revision"
        echo "Command that was run:"
        echo $SUPOSE_CMD
        exit $SUPOSE_STATUS;
fi

# If no error, update last.rev file
echo "**** Operation completed successfully ****"
echo "Updating $LASTREV_FILE with revision: $TOREV"
echo "$TOREV" > "$LASTREV_FILE"
exit 0
</pre>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/10/supose-bash-update-script-cron/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

