<?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>Source Allies Blog &#187; oracle</title>
	<atom:link href="http://blogs.sourceallies.com/tag/oracle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:40:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Incrementing Oracle Sequences Without Permissions</title>
		<link>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/</link>
		<comments>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 19:06:52 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1499</guid>
		<description><![CDATA[If you are ever working with a database where data is consistently being imported, dropped, and/or restored; sooner or later you will find yourself in the situation where the primary key of a table is no longer synchronized with the sequence used to generate it.

If you have the appropriate permissions, this is easy to fix. [...]]]></description>
			<content:encoded><![CDATA[<p>If you are ever working with a database where data is consistently being imported, dropped, and/or restored; sooner or later you will find yourself in the situation where the primary key of a table is no longer synchronized with the sequence used to generate it.<br />
<span id="more-1499"></span><br />
If you have the appropriate permissions, this is easy to fix.  Say you need to advance the sequence by 500, you just execute the following sql:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">ALTER</span> sequence MY_SEQ increment <span style="color: #00F;">BY</span> <span style="color: #800;">500</span><span style="color: #00F;">;</span>
<span style="color: #00F;">SELECT</span> MY_SEQ<span style="color: #00F;">.</span><span style="color: #00F;">NEXTVAL</span> <span style="color: #00F;">FROM</span> dual<span style="color: #00F;">;</span>
<span style="color: #00F;">ALTER</span> sequence MY_SEQ increment <span style="color: #00F;">BY</span> <span style="color: #800;">1</span><span style="color: #00F;">;</span></pre></div></div>

<p>What do you do if you don&#8217;t have the correct permissions to do this?  You can always call up the dba and ask him.  If you&#8217;re impatient, you can also do a little pl/sql programming to accomplish the same thing.  All you want to do is just repeatedly select the next value until the sequence value is higher that the current key value.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">DECLARE</span>
  current_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
  seq_val <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
<span style="color: #00F;">BEGIN</span>
  <span style="color: #00F;">SELECT</span> <span style="color: #000;">MAX</span><span style="color: #00F;">&#40;</span>MY_TABLE<span style="color: #00F;">.</span>id<span style="color: #00F;">&#41;</span> <span style="color: #00F;">INTO</span> current_val <span style="color: #00F;">FROM</span> MY_TABLE<span style="color: #00F;">;</span>
  <span style="color: #00F;">WHILE</span> current_val <span style="color: #00F;">&gt;=</span> seq_val
  <span style="color: #00F;">LOOP</span>
    <span style="color: #00F;">SELECT</span> MY_SEQ<span style="color: #00F;">.</span><span style="color: #00F;">NEXTVAL</span> <span style="color: #00F;">INTO</span> seq_val <span style="color: #00F;">FROM</span> dual<span style="color: #00F;">;</span>
  <span style="color: #00F;">END</span> <span style="color: #00F;">LOOP</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span><span style="color: #00F;">;</span></pre></div></div>

<p>Of course, if you encounter this situation once, chances are that you will encounter it many times.  Next, you can create a function that takes the table name, key column name, and sequence name and then executes dynamically created sql to fix the problem.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">PROCEDURE</span> INCREMENT_SEQ<span style="color: #00F;">&#40;</span><span style="color: #00F;">IN</span> TABLE_NAME<span style="color: #00F;">,</span> <span style="color: #00F;">IN</span> ID_COLUMN_NAME<span style="color: #00F;">,</span> <span style="color: #00F;">IN</span> SEQUENCE_NAME<span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
<span style="color: #00F;">DECLARE</span>
  current_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
  seq_val <span style="color: #00F;">NUMBER</span> <span style="color: #00F;">:=</span> <span style="color: #00F;">-</span><span style="color: #800;">1</span><span style="color: #00F;">;</span>
<span style="color: #00F;">BEGIN</span>
  <span style="color: #00F;">EXECUTE</span> <span style="color: #00F;">IMMEDIATE</span> <span style="color: #F00;">'select max('</span> <span style="color: #00F;">||</span> TABLE_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">'.'</span> <span style="color: #00F;">||</span> ID_COLUMN_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">') into current value from '</span> <span style="color: #00F;">||</span> TABLE_NAME<span style="color: #00F;">;</span>
  <span style="color: #00F;">WHILE</span> current_val <span style="color: #00F;">&gt;=</span> seq_val
  <span style="color: #00F;">LOOP</span>
    <span style="color: #00F;">EXECUTE</span> <span style="color: #00F;">IMMEDIATE</span> <span style="color: #F00;">'select '</span> <span style="color: #00F;">||</span> SEQUENCE_NAME <span style="color: #00F;">||</span> <span style="color: #F00;">&quot;.nextval into seq_val from dual';
  end loop;
END INCREMENT_SEQ;</span></pre></div></div>

<p>Now, you should be able to fix any sequence/key mismatches simply by calling the procedure.</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;">INCREMENT_SEQ<span style="color: #00F;">&#40;</span><span style="color: #F00;">'MY_TABLE'</span><span style="color: #00F;">,</span> <span style="color: #F00;">'ID'</span><span style="color: #00F;">,</span> <span style="color: #F00;">'MY_SEQ'</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/04/incrementing-oracle-sequences-without-permissions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PL/SQL Variables and Connection Pooling</title>
		<link>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/</link>
		<comments>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 16:56:44 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[lessons]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[pl/sql]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=413</guid>
		<description><![CDATA[I recently had to implement a common feature across multiple applications and app servers, all of which point to the same Oracle database.  For reasons unrelated, I chose to implement this feature using PL/SQL.  You can all stop laughing now.  I ended up with something resembling:

CREATE OR REPLACE PACKAGE BODY MY_PKG IS
 [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to implement a common feature across multiple applications and app servers, all of which point to the same Oracle database.  For reasons unrelated, I chose to implement this feature using PL/SQL.  You can all stop laughing now.  I ended up with something resembling:</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">CREATE</span> <span style="color: #00F;">OR</span> <span style="color: #000;">REPLACE</span> <span style="color: #00F;">PACKAGE</span> <span style="color: #00F;">BODY</span> MY_PKG <span style="color: #00F;">IS</span>
   g_enabled <span style="color: #00F;">BOOLEAN</span> <span style="color: #00F;">:=</span> <span style="color: #00F;">TRUE</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">PROCEDURE</span> enable_sp<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      <span style="color: #00F;">BEGIN</span>
         g_enabled <span style="color: #00F;">:=</span> <span style="color: #00F;">TRUE</span><span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">PROCEDURE</span> disable_sp<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      <span style="color: #00F;">BEGIN</span>
         g_enabled <span style="color: #00F;">:=</span> <span style="color: #00F;">FALSE</span><span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
&nbsp;
   <span style="color: #00F;">FUNCTION</span> is_enabled_fn<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span> <span style="color: #00F;">IS</span>
      l_return_value <span style="color: #00F;">NUMBER</span><span style="color: #00F;">;</span>
&nbsp;
      <span style="color: #00F;">BEGIN</span>
         <span style="color: #00F;">IF</span> g_enabled <span style="color: #00F;">THEN</span>
            l_return_value <span style="color: #00F;">:=</span> <span style="color: #800;">1</span><span style="color: #00F;">;</span>
         <span style="color: #00F;">ELSE</span>
            l_return_value <span style="color: #00F;">:=</span> <span style="color: #800;">0</span><span style="color: #00F;">;</span>
         <span style="color: #00F;">END</span> <span style="color: #00F;">IF</span><span style="color: #00F;">;</span>
&nbsp;
         <span style="color: #00F;">RETURN</span> l_return_value<span style="color: #00F;">;</span>
      <span style="color: #00F;">END</span><span style="color: #00F;">;</span>
<span style="color: #00F;">END</span> MY_PKG<span style="color: #00F;">;</span></pre></div></div>

<p>Seems pretty innocent, doesn&#8217;t it?</p>
<p>So, I write unit tests in java to test everything.  It all seems to be working.  It goes through code review.  Nothing more serious than cosmetic issues were found.  It goes through the qa cycles.  It all seems to be working.  It gets deployed to production.  At first, it seems to be working.  Then I start to see some interesting behavior.</p>
<h3>Lesson #1:</h3>
<p>PL/SQL data types aren&#8217;t as straight forward as I would have liked.  The variable g_enabled is of type BOOLEAN.  This is a data type built into PL/SQL.  It is not a data type in Oracle&#8217;s version of SQL.  Why is this significant?  Consider the two following scenario&#8217;s and their result (assuming that is_enabled=TRUE) -</p>

<div class="wp_syntax"><div class="code"><pre class="plsql" style="font-family:monospace;"><span style="color: #00F;">BEGIN</span>
&nbsp;
  <span style="color: #00F;">DBMS_OUTPUT</span><span style="color: #00F;">.</span>put_line<span style="color: #00F;">&#40;</span>my_pkg<span style="color: #00F;">.</span>is_enabled_fn<span style="color: #00F;">&#40;</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">&#41;</span><span style="color: #00F;">;</span>
&nbsp;
<span style="color: #00F;">END</span><span style="color: #00F;">;</span></pre></div></div>

<p>This will print out a 1 to the standard output.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> my_pkg<span style="color: #66cc66;">.</span>is_enabled_fn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> dual;</pre></div></div>

<p>This query will return 0.</p>
<p>This was not what I expected.  I would have expected the second query to return a 1.  The reason it doesn&#8217;t, is that the query is not run in a PL/SQL block.  This results in the BOOLEAN data type to not be defined and the if statement in is_enabled_fn() to always evaluate to false.</p>
<h3>Lesson #2:</h3>
<p>Package variables are connection private.  I create two connections, A and B, to the database.  If I call disable_sp() in connection A, is_enabled_fn() will still return a 1 in connection B.  The value of the variable is not shared across the connections.  If I were then to create a connection C, is_enabled_fn() will return 0.  This is because connection C was created after the value was set in connection A. While it wasn&#8217;t what I initially expected, it makes some sense in that the connections don&#8217;t share memory.  The really big problem comes into play when app servers pool connections and might not close them for weeks at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/11/plsql-variables-and-connection-pooling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

