Pages

Monday, October 15, 2012

Locking Table [Tablock vs Tablocks]

If you are in a transaction and you grab an exclusive lock on a table, EG:

SELECT 1 FROM TABLE WITH (TABLOCKX)

No other processes will be able to grab any locks on the table, meaning all queries attempting to talk to the table will be blocked until the transaction commits.

TABLOCK only grabs a shared lock, shared locks are released after a statement is executed if your transaction isolation is READ COMMITTED (default). If your isolation level is higher, for example:SERIALIZABLE, shared locks are held until the end of a transaction.

Shared locks are, hmmm, shared. Meaning 2 transactions can both read data from the table at the same time if they both hold a S or IS lock on the table (via TABLOCK). However, if transaction A holds a shared lock on a table, transaction B will not be able to grab an exclusive lock until all shared locks are released. Read about which locks are compatible with which at msdn.

Both hints cause the db to bypass taking more granular locks (like row or page level locks). In principle, more granular locks allow you better concurrency. So for example, one transaction could be updating row 100 in your table and another row 1000, at the same time from two transactions (it gets tricky with page locks, but lets skip that).

In general granular locks is what you want, but sometimes you may want to reduce db concurrency to increase performance of a particular operation and eliminate the chance of deadlocks.

In general you would not use TABLOCK or TABLOCKX unless you absolutely needed it for some edge case.

Quite an old article on mssqlcity attempts to explain the types of locks:
Shared locks are used for operations that do not change or update data, such as a SELECT statement.
Update locks are used when SQL Server intends to modify a page, and later promotes the update page lock to an exclusive page lock before actually making the changes.
Exclusive locks are used for the data modification operations, such as UPDATE, INSERT, or DELETE.
What it doesn't discuss are Intent (which basically is a modifier for these lock types). Intent (Shared/Exclusive) locks are locks held at a higher level than the real lock. So, for instance, if your transaction has an X lock on a row, it will also have an IX lock at the table level (which stops other transactions from attempting to obtain an X lock on the table until your transaction completes or rolls back).

The concept of "sharing" a lock is quite straightforward - multiple transactions can have a Shared lock for the same resource, whereas only a single transaction may have an Exclusive lock, and an Exclusive lock precludes any transaction from obtaining or holding a Shared lock.

Exclusive locks are always held until the end of the transaction.

all shared locks are released at the end of a statement in a read committed transaction, if you want to hold the locks you need hints or a higher isolation level. tablockx will grab an X lock.


Just collecting notes and sharing..
Source: www.stackoverflow.com

Wednesday, August 15, 2012

Makanan yang melemahkan Tulang

Ghiboo.com - Selain mengetahui makanan mana saja yang memperkuat tulang, kita juga perlu tahu makanan yang bisa merusak tulang.

Bisa saja, beberapa makanan favorit Anda justru melemahkan kekuatan tulang Anda. Yuk, cari tahu agar Anda bisa membatasi konsumsinya sebelum terlambat.

Makanan asin dengan garam tinggi

Makanan olahan atau makanan asin membuat kalsium dalam tulang menghilang. Hal ini terjadi karena garam dapat menyebabkan ekskresi kalsium yang berlebihan melalui ginjal. Sebaiknya, asupan garam tak lebih dari 1.200 mg per hari.

Makanan manis

Ternyata makanan manis tak hanya memengaruhi kesehatan gigi saja, tetapi juga menimbulkan masalah baru pada tulang Anda. Kelebihan gula dapat menghambat penyerapan kalsium dan mengikis fosfor. Jika Anda menyukai makanan manis, ganti dengan buah-buahan dan hindari makanan olahan.

Cola

Minuman bersoda mengurangi kepadatan mineral tulang yang meningkatkan risiko patah tulang. Cola mengandung asam folat yang dapat melemahkan usus dan penyerapan kalsium.

Kafein

Kafein dapat melarutkan kalsium dalam tulang dan melemahkan fungsinya. Sekitar 6 miligram kalsium akan hilang setiap kali Anda mengonsumsi 100 mg kafein. Terlebih lagi, jika ditambahkan gula yang malah memberikan efek merugikan bagi kesehatan tulang.

Alkohol

Alkohol berkontribusi pada rendahnya massa tulang, penurunan pembentukan tulang, meningkatkan risiko patah tulang dan menghambat pemulihan patah tulang.

Kacang

Hampir semua kacang-kacangan mengandung phytates. Sayangnya, hampir semua kacang-kacangan mengandung phytate. Phytate dapat mengganggu kemampuan tubuh untuk menyerap kalsium. Tapi, sebaiknya tak perlu takut dan memilih menghindari kacang. Makanan ini mengandung magnesium, serat dan nutrisi lainnya yang diperlukan tubuh. Untuk mengatasinya, pengurangan tingkat phytate dapat dilakukan dengan merendam biji kacang dalam air beberapa jam sebelum direbus.

Just collecting notes and sharing..

Wednesday, June 6, 2012

Java Notes: Comparable dan Comparator

Comparable - java.lang package

@ this.compateTo(object o)
 same -> = 0
 less than -> <0
 greater than -> >0

Comparator - java.util package

@compare(object o1, object o2)
 same -> = 0
 less than -> <0
 greater than -> >0

Both are interface.
The different is "this" reference

Comparable is used by most java class like String, Date, Array, wrapper classes, etc
Comparable implement natural ordering of object.
If any object implements Comparable and collection of that object is either List or Array can be sorted automatically.

Oject that implement Comparable in Java can be used as keys in sorted map or elements in a sorted set. Eg. TreeSet, without specifiying any Comparator


Just collecting notes & sharing..
Please let me know if there are wrong statements in this article or a copy-paste one.

Saturday, May 19, 2012

About Color Banding

Do you know about Color banding?

If you are working in design animation, video rendering, digital coloring and its friends, you must know well about this, otherwise you may unfamiliar with this. The only reason I post about this one because somehow it is interested me.

After read about the newest video encoding in 10bit depth, I accidentally know about what is the color banding is. I get this from watching my favorite anime with 10bit depth encoding. That's how I get know about color banding.

Color Banding sample


Banding happens when video is compressed or decompressed and similar colors get posterized. This undesirable, visible bands of color that can be distracting, and in some cases downright ugly. It can happen in many situation, like video, picture or games. If it is in a small area, we wont notice and nobody got annoyed. But if we see in a large area, a large screen, it will very-very disturbing our eyes.

There are quite a few ways to remove color banding. Everyone seems to have their own formula.And for those kind of matters, I dont have any knowledge to explain it in here.



Just collecting notes and sharing.. Please let me know if there are wrong statements in this article or a copy-paste one.

Thursday, May 17, 2012

Comparing Date in Java


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HashtableDemo {

    public static void main(String args[]) throws AssertionError, ParseException {

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

        //comparing date using compareTo method in Java
        System.out.println("Comparing two Date in Java using CompareTo method");
        compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

        //comparing dates in java using Date.before, Date.after and Date.equals
        System.out.println("Comparing two Date in Java using Date's before, after and equals method");
        compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

        //comparing dates in java using Calendar.before(), Calendar.after and Calendar.equals()
        System.out.println("Comparing two Date in Java using Calendar's before, after and equals method");
        compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

    }

    public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate) {
        //how to check if date1 is equal to date2
        if (oldDate.compareTo(newDate) == 0) {
            System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
        }

        //checking if date1 is less than date 2
        if (oldDate.compareTo(newDate) < 0) {
            System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
        }

        //how to check if date1 is greater than date2 in java
        if (oldDate.compareTo(newDate) > 0) {
            System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
        }
    }

    public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate) {
        //how to check if two dates are equals in java
        if (oldDate.equals(newDate)) {
            System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
        }

        //checking if date1 comes before date2
        if (oldDate.before(newDate)) {
            System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
        }

        //checking if date1 comes after date2
        if (oldDate.after(newDate)) {
            System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
        }
    }

    public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate) {

        //creating calendar instances for date comparision
        Calendar oldCal = Calendar.getInstance();
        Calendar newCal = Calendar.getInstance();

        oldCal.setTime(oldDate);
        newCal.setTime(newDate);

        //how to check if two dates are equals in java using Calendar
        if (oldCal.equals(newCal)) {
            System.out.println(df.format(oldDate) + " and " + df.format(newDate) + " are equal to each other");
        }

        //how to check if one date comes before another using Calendar
        if (oldCal.before(newCal)) {
            System.out.println(df.format(oldDate) + " comes before " + df.format(newDate));
        }

        //how to check if one date comes after another using Calendar
        if (oldCal.after(newCal)) {
            System.out.println(df.format(oldDate) + " comes after " + df.format(newDate));
        }
    }
}

Output:

Comparing two Date in Java using CompareTo method
01-01-2012 and 01-01-2012 are equal to each other
02-03-2012 is less than 04-05-2012
02-03-2012 is greater than 01-02-2012
Comparing two Date in Java using Date's before, after and equals method
01-01-2012 and 01-01-2012 are equal to each other
02-03-2012 comes before 04-05-2012
02-03-2012 comes after 01-02-2012
Comparing two Date in Java using Calendar's before, after and equals method
01-01-2012 and 01-01-2012 are equal to each other
02-03-2012 comes before 04-05-2012
02-03-2012 comes after 01-02-2012


Source: roseIndia.net

Just collecting notes & sharing..

Sunday, May 13, 2012

ConcurrentHashMap and hashTable

So what is the difference between hashtable and ConcurrentHashMap , both can be used in multithreaded environment but once the size of hashtable becomes considerable larg,e performance degrade because for iteration it has to be locked for longer duration.

Since ConcurrentHashMap indroduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete.

In Summary ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration.

Just collecting notes and sharing..
Please let me know if there're wrong statements in this article or copy-paste one.

Monday, May 7, 2012

Tertawa itu sehat dan menguntungkan

Setiap orang pasti akan tertawa beberapa kali setiap hari. Mendengarkan lelucon, menonton film komedi atau membaca cerita lucu dengan mudah membuat kita tertawa.
Tertawa ternyata tak hanya mengungkapkan perasaan bahagia, namun juga membuat tubuh menjadi lebih sehat. Berikut ini beberapa manfaat dari tertawa yang bisa Anda rasakan bagi kesehatan.

1. Tertawa membantu meningkatkan sistem kekebalan tubuh dengan meningkatkan jumlah antibodi yang memproduksi sel T. Sel-sel ini membantu tubuh melawan virus dan tumor.

2. Tertawa membantu penderita hipertensi karena dapat menurunkan tekanan darah. Tertawa juga mengurangi kadar hormon kartisol yang menghalangi proses penyembuhan penyakit dan juga membantu menstabilkan tingkat gula darah.

3. Tertawa merangsang perubahan zat kimia dalam otak yang membantu menahan tubuh terhadap efek kumulatif dari stres.

4. Tertawa membantu membakar kalori. Menurut penelitian, tertawa 10-15 menit setiap hari dapat membakar kalori hingga 40 kalori. Tertawa juga meningkatkan denyut jantung dan mempercepat metabolisme yang memengaruhi berat badan.

5. Tertawa merangsang otak untuk memproduksi hormon endorfin, yang membantu menaikkan suasana hati seseorang dan membuat perasaan bahagia.

6. Tertawa membantu mengurangi peradangan di seluruh tubuh, sehingga baik bagi kesehatan jantung, otak dan peredaran darah.

7. Tertawa menjadi 'pijat' bagi organ internal karena efeknya mirip dengan melakukan olahraga.

8. Tertawa juga memberikan latihan ringan untuk jantung, paru-paru, diagfragma dan bahkan otot-otot perut.

9. Tertawa membantu melepaskan ketegangan di otot-otot wajah, leher, bahu dan perut, dimana semua bagian tubuh ini sering mengalami ketegangan akibat rutinitas sehari-hari.

10. Tertawa itu menular. Orang yang tertawa atau melucu dengan membuat lelucon akan mengundang tawa sehingga meningkatkan semangat dan menciptakan energi positif bagi orang-orang sekitarnya.

Just collecting notes and sharing..
Source: Ghiboo.com