SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Week 6
Recap: Transactions
    2 Transactions running concurrently In 2 separate terminals

1                                          2
begin work;

Update mytest set                          Update mytest set val1 =
val1 = “Transaction                        “another update” where
update” where val2 =
“adsf”;                                    val2 = “adsf”;

Commit;
                                           Waits for transaction 1 to commit
Recap: rollback;
BEGIN WORK;

update products set
prod_name = "iphone"
where id = "1”;

Select * from mytest

rollback;

Commit;

SELECT * mytest
PHP




Useful easy to hack together server side language
Useful- but don’t stress out trying to get to 100% understand it in this
module
Testing PHP
• Turn on Apache and MySQL in XAMPP / MAMP /
  WAMP
• Use IDE of choice (eclipse, notepad, notepad ++)

<?php
Print "Hello, World!";
?>

• Save to htdocs
PDO
• Class that represents a connection between PHP and a
  database server
   – (class is like a blueprint of how to construct something)
• Does not have to be mysql: Can be other databases
  systems
• Used to use mysql extension which will be removed
  soon <- lots of examples will use this!
• Good site explaining why nobody uses mysql extension
  anymore: http://net.tutsplus.com/tutorials/php/php-
  database-access-are-you-doing-it-correctly/


Phptherightway.com
PDO
Connection
<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

$host= "localhost";
$dbname = "shop";
$user = "root";
$pass = "root";

try {
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}

catch(PDOException $e) {
  echo $e->getMessage();
}

?>
Selecting * from


id = 1;
try {
   $pdo= new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $stmt = $pdo->prepare('SELECT * FROM products WHERE id = :id');
   $stmt->execute(array('id' => $id));
   while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      print_r($row);
   }
} catch(PDOException $e) {
   echo 'ERROR: ' . $e->getMessage();
}
Insert into

try {
  $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $pdo->prepare("INSERT INTO products (prod_name, prod_desc, supplier_id)VALUES(:prod_name, :prod_desc, :supplier_id)");
  $stmt->execute(array(
      ':prod_name' => 'test',
      ':prod_desc' => 'test',
      ':supplier_id' => '1'
  ));
  # Affected Rows?
  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}
Transactions in PDO
try {
 $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

$pdo->beginTransaction();

 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $pdo->prepare("INSERT INTO products (prod_name, prod_desc, supplier_id) VALUES(:prod_name, :prod_desc, :supplier_id)");
 $stmt->execute(array(
     ':prod_name' => 'test',
     ':prod_desc' => 'test',
     ':supplier_id' => '1'
 ));

$pdo->rollBack();
//$pdo->commit();

 # Affected Rows?

  echo $stmt->rowCount(); // 1
} catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}
Where is the transaction stored, in this
               example?
Really boring transaction stuff Mark
           left for me to do.

Transaction: a unit of work performed within a
database management system
Properties of reliable transactions..
• In relational databases transactions should
  always have:

•   Atomicity
•   Consistency
•   Isolation
•   Durability
Atomicity
• Each transaction is all or nothing..
• It either does everything its supposed to do,
  or nothing at all!
Consistency
• All data in the database is valid
• Any rules the database has must not be
  broken!
Isolation
• Running transactions do not interfere with
  each other
• Transactions that run at the same time is no
  different from transactions ran one after
  another
Durability
• All transactions will survive after a commit,
  even after a hardware failure
Exercise
• Use PHP to simulate thousands of users of
  users accessing 1 resource
Playing: Stress testing
• Jmeter
• Windows:
  http://zacster.blogspot.co.uk/2008/03/quichtt
  p://biscminds.blogspot.co.uk/2011/12/quick-
  jmeter-setup-on-mac.html-howto-to-setup-
  jmeter.html
• Mac:
  http://biscminds.blogspot.co.uk/2011/12/quic
  k-jmeter-setup-on-mac.html
Next week: Triggers

Weitere ähnliche Inhalte

Was ist angesagt?

Metis - RubyConf 2011 Lightning Talk
Metis - RubyConf 2011 Lightning TalkMetis - RubyConf 2011 Lightning Talk
Metis - RubyConf 2011 Lightning TalkKen Robertson
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3mihirio
 
Redis - Usability and Use Cases
Redis - Usability and Use CasesRedis - Usability and Use Cases
Redis - Usability and Use CasesFabrizio Farinacci
 
Unit testing powershell
Unit testing powershellUnit testing powershell
Unit testing powershellMatt Wrock
 
Service Functions
Service FunctionsService Functions
Service Functionspineda2
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.jsWebsecurify
 
Pycon 2012 Apache Cassandra
Pycon 2012 Apache CassandraPycon 2012 Apache Cassandra
Pycon 2012 Apache Cassandrajeremiahdjordan
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v Arian Gutierrez
 
node.js Module Development
node.js Module Developmentnode.js Module Development
node.js Module DevelopmentJay Harris
 
Test driven development (java script & mivascript)
Test driven development (java script & mivascript)Test driven development (java script & mivascript)
Test driven development (java script & mivascript)Miva
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
Beginning Jquery In Drupal Theming
Beginning Jquery In Drupal ThemingBeginning Jquery In Drupal Theming
Beginning Jquery In Drupal ThemingRob Knight
 

Was ist angesagt? (20)

Metis - RubyConf 2011 Lightning Talk
Metis - RubyConf 2011 Lightning TalkMetis - RubyConf 2011 Lightning Talk
Metis - RubyConf 2011 Lightning Talk
 
hacking with node.JS
hacking with node.JShacking with node.JS
hacking with node.JS
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3
 
Redis - Usability and Use Cases
Redis - Usability and Use CasesRedis - Usability and Use Cases
Redis - Usability and Use Cases
 
Unit testing powershell
Unit testing powershellUnit testing powershell
Unit testing powershell
 
What is nodejs
What is nodejsWhat is nodejs
What is nodejs
 
Service Functions
Service FunctionsService Functions
Service Functions
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
 
Pycon 2012 Apache Cassandra
Pycon 2012 Apache CassandraPycon 2012 Apache Cassandra
Pycon 2012 Apache Cassandra
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
Implementing New Web
Implementing New WebImplementing New Web
Implementing New Web
 
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
 
node.js Module Development
node.js Module Developmentnode.js Module Development
node.js Module Development
 
Test driven development (java script & mivascript)
Test driven development (java script & mivascript)Test driven development (java script & mivascript)
Test driven development (java script & mivascript)
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Fluent plugin-dstat
Fluent plugin-dstatFluent plugin-dstat
Fluent plugin-dstat
 
Beginning Jquery In Drupal Theming
Beginning Jquery In Drupal ThemingBeginning Jquery In Drupal Theming
Beginning Jquery In Drupal Theming
 

Andere mochten auch (17)

Week3 adb
Week3 adbWeek3 adb
Week3 adb
 
Flashtalk david
Flashtalk davidFlashtalk david
Flashtalk david
 
Aplikasi determinan dalam geometri
Aplikasi determinan dalam geometriAplikasi determinan dalam geometri
Aplikasi determinan dalam geometri
 
χοροι
χοροιχοροι
χοροι
 
Revision
RevisionRevision
Revision
 
Agent Based Models
Agent Based ModelsAgent Based Models
Agent Based Models
 
Recognizing Human-Object Interactions in Still Images by Modeling the Mutual ...
Recognizing Human-Object Interactions inStill Images by Modeling the Mutual ...Recognizing Human-Object Interactions inStill Images by Modeling the Mutual ...
Recognizing Human-Object Interactions in Still Images by Modeling the Mutual ...
 
public cloud security via ids
public cloud security via idspublic cloud security via ids
public cloud security via ids
 
Soa
SoaSoa
Soa
 
Dos presentation by ahlam shakeel
Dos presentation by ahlam shakeelDos presentation by ahlam shakeel
Dos presentation by ahlam shakeel
 
Html5 offers 5 times better ways to hijack the website
Html5 offers 5 times better ways to hijack the website Html5 offers 5 times better ways to hijack the website
Html5 offers 5 times better ways to hijack the website
 
grid authentication
grid authenticationgrid authentication
grid authentication
 
Rbac
RbacRbac
Rbac
 
Operating system vulnerability and control
Operating system vulnerability and control Operating system vulnerability and control
Operating system vulnerability and control
 
Image based authentication
Image based authenticationImage based authentication
Image based authentication
 
Dmz
Dmz Dmz
Dmz
 
Grid computing by ahlam ansari
Grid computing by  ahlam ansariGrid computing by  ahlam ansari
Grid computing by ahlam ansari
 

Ähnlich wie Week6

Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetAchieve Internet
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Eric Hogue
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testingPeter Edwards
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckrICh morrow
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applicationschartjes
 
From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011Carlos Sanchez
 
From Dev to DevOps
From Dev to DevOpsFrom Dev to DevOps
From Dev to DevOpsAgile Spain
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back AgainPuppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back AgainPuppet
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Michelangelo van Dam
 
Puppet for Developers
Puppet for DevelopersPuppet for Developers
Puppet for Developerssagarhere4u
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShellBoulos Dib
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012Carlos Sanchez
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboardsDenis Ristic
 
Tips
TipsTips
Tipsmclee
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13Dave Gardner
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Jason Lotito
 

Ähnlich wie Week6 (20)

Harmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and PuppetHarmonious Development: Via Vagrant and Puppet
Harmonious Development: Via Vagrant and Puppet
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testing
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011From Dev to DevOps - ApacheCON NA 2011
From Dev to DevOps - ApacheCON NA 2011
 
From Dev to DevOps
From Dev to DevOpsFrom Dev to DevOps
From Dev to DevOps
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back AgainPuppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
Fatc
FatcFatc
Fatc
 
Puppet for Developers
Puppet for DevelopersPuppet for Developers
Puppet for Developers
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012From Dev to DevOps - Codemotion ES 2012
From Dev to DevOps - Codemotion ES 2012
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards
 
Tips
TipsTips
Tips
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Planning to Fail #phpne13
Planning to Fail #phpne13Planning to Fail #phpne13
Planning to Fail #phpne13
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
 
Pecl Picks
Pecl PicksPecl Picks
Pecl Picks
 

Week6

  • 2. Recap: Transactions 2 Transactions running concurrently In 2 separate terminals 1 2 begin work; Update mytest set Update mytest set val1 = val1 = “Transaction “another update” where update” where val2 = “adsf”; val2 = “adsf”; Commit; Waits for transaction 1 to commit
  • 3. Recap: rollback; BEGIN WORK; update products set prod_name = "iphone" where id = "1”; Select * from mytest rollback; Commit; SELECT * mytest
  • 4. PHP Useful easy to hack together server side language Useful- but don’t stress out trying to get to 100% understand it in this module
  • 5. Testing PHP • Turn on Apache and MySQL in XAMPP / MAMP / WAMP • Use IDE of choice (eclipse, notepad, notepad ++) <?php Print "Hello, World!"; ?> • Save to htdocs
  • 6. PDO • Class that represents a connection between PHP and a database server – (class is like a blueprint of how to construct something) • Does not have to be mysql: Can be other databases systems • Used to use mysql extension which will be removed soon <- lots of examples will use this! • Good site explaining why nobody uses mysql extension anymore: http://net.tutsplus.com/tutorials/php/php- database-access-are-you-doing-it-correctly/ Phptherightway.com
  • 7. PDO
  • 8. Connection <?php error_reporting(E_ALL); ini_set("display_errors", 1); $host= "localhost"; $dbname = "shop"; $user = "root"; $pass = "root"; try { $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); } catch(PDOException $e) { echo $e->getMessage(); } ?>
  • 9. Selecting * from id = 1; try { $pdo= new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare('SELECT * FROM products WHERE id = :id'); $stmt->execute(array('id' => $id)); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { print_r($row); } } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); }
  • 10. Insert into try { $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare("INSERT INTO products (prod_name, prod_desc, supplier_id)VALUES(:prod_name, :prod_desc, :supplier_id)"); $stmt->execute(array( ':prod_name' => 'test', ':prod_desc' => 'test', ':supplier_id' => '1' )); # Affected Rows? echo $stmt->rowCount(); // 1 } catch(PDOException $e) { echo 'Error: ' . $e->getMessage(); }
  • 11. Transactions in PDO try { $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $pdo->beginTransaction(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->prepare("INSERT INTO products (prod_name, prod_desc, supplier_id) VALUES(:prod_name, :prod_desc, :supplier_id)"); $stmt->execute(array( ':prod_name' => 'test', ':prod_desc' => 'test', ':supplier_id' => '1' )); $pdo->rollBack(); //$pdo->commit(); # Affected Rows? echo $stmt->rowCount(); // 1 } catch(PDOException $e) { echo 'Error: ' . $e->getMessage(); }
  • 12. Where is the transaction stored, in this example?
  • 13. Really boring transaction stuff Mark left for me to do. Transaction: a unit of work performed within a database management system
  • 14. Properties of reliable transactions.. • In relational databases transactions should always have: • Atomicity • Consistency • Isolation • Durability
  • 15. Atomicity • Each transaction is all or nothing.. • It either does everything its supposed to do, or nothing at all!
  • 16. Consistency • All data in the database is valid • Any rules the database has must not be broken!
  • 17. Isolation • Running transactions do not interfere with each other • Transactions that run at the same time is no different from transactions ran one after another
  • 18. Durability • All transactions will survive after a commit, even after a hardware failure
  • 19. Exercise • Use PHP to simulate thousands of users of users accessing 1 resource
  • 20. Playing: Stress testing • Jmeter • Windows: http://zacster.blogspot.co.uk/2008/03/quichtt p://biscminds.blogspot.co.uk/2011/12/quick- jmeter-setup-on-mac.html-howto-to-setup- jmeter.html • Mac: http://biscminds.blogspot.co.uk/2011/12/quic k-jmeter-setup-on-mac.html