1
- // Copyright © 2004, 2013 , Oracle and/or its affiliates. All rights reserved.
1
+ // Copyright © 2004, 2014 , Oracle and/or its affiliates. All rights reserved.
2
2
//
3
3
// MySQL Connector/NET is licensed under the terms of the GPLv2
4
4
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
43
43
#endif
44
44
#if NET_40_OR_GREATER
45
45
using System . Threading . Tasks ;
46
+ using System . Threading ;
46
47
#endif
47
48
48
49
namespace MySql . Data . MySqlClient
@@ -88,6 +89,17 @@ public MySqlConnection(string connectionString)
88
89
ConnectionString = connectionString ;
89
90
}
90
91
92
+ #region Destructor
93
+ ~ MySqlConnection ( )
94
+ {
95
+ #if ! RT
96
+ Dispose ( false ) ;
97
+ #else
98
+ Dispose ( ) ;
99
+ #endif
100
+ }
101
+ #endregion
102
+
91
103
#region Interal Methods & Properties
92
104
93
105
#if ! CF
@@ -478,7 +490,6 @@ public override void Open()
478
490
if ( driver == null )
479
491
{
480
492
ReplicationManager . GetNewConnection ( Settings . Server , false , this ) ;
481
- return ;
482
493
}
483
494
else
484
495
currentSettings = driver . Settings ;
@@ -802,11 +813,21 @@ internal void Throw(Exception ex)
802
813
#endif
803
814
}
804
815
816
+ #if ! RT
817
+ public void Dispose ( )
818
+ {
819
+ Dispose ( true ) ;
820
+ GC . SuppressFinalize ( this ) ;
821
+ }
822
+ #else
805
823
public void Dispose ( )
806
824
{
807
825
if ( State == ConnectionState . Open )
808
826
Close ( ) ;
827
+
828
+ GC . SuppressFinalize ( this ) ;
809
829
}
830
+ #endif
810
831
811
832
#if NET_40_OR_GREATER
812
833
#region Async
@@ -816,76 +837,179 @@ public void Dispose()
816
837
/// <returns>An object representing the new transaction.</returns>
817
838
public Task < MySqlTransaction > BeginTransactionAsync ( )
818
839
{
819
- return BeginTransactionAsync ( IsolationLevel . RepeatableRead ) ;
840
+ return BeginTransactionAsync ( IsolationLevel . RepeatableRead , CancellationToken . None ) ;
841
+ }
842
+
843
+ public Task < MySqlTransaction > BeginTransactionAsync ( CancellationToken cancellationToken )
844
+ {
845
+ return BeginTransactionAsync ( IsolationLevel . RepeatableRead , cancellationToken ) ;
820
846
}
847
+
821
848
/// <summary>
822
849
/// Async version of BeginTransaction
823
850
/// </summary>
824
851
/// <param name="iso">The isolation level under which the transaction should run. </param>
825
852
/// <returns>An object representing the new transaction.</returns>
826
853
public Task < MySqlTransaction > BeginTransactionAsync ( IsolationLevel iso )
827
854
{
828
- return Task . Factory . StartNew ( ( ) =>
855
+ return BeginTransactionAsync ( iso , CancellationToken . None ) ;
856
+ }
857
+
858
+ public Task < MySqlTransaction > BeginTransactionAsync ( IsolationLevel iso , CancellationToken cancellationToken )
859
+ {
860
+ var result = new TaskCompletionSource < MySqlTransaction > ( ) ;
861
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
829
862
{
830
- return BeginTransaction ( iso ) ;
831
- } ) ;
863
+ try
864
+ {
865
+ MySqlTransaction tranResult = BeginTransaction ( iso ) ;
866
+ result . SetResult ( tranResult ) ;
867
+ }
868
+ catch ( Exception ex )
869
+ {
870
+ result . SetException ( ex ) ;
871
+ }
872
+ }
873
+ else
874
+ {
875
+ result . SetCanceled ( ) ;
876
+ }
877
+
878
+ return result . Task ;
832
879
}
833
- /// <summary>
834
- /// Async version of ChangeDataBase
835
- /// </summary>
836
- /// <param name="databaseName">The name of the database to use.</param>
837
- /// <returns></returns>
880
+
838
881
public Task ChangeDataBaseAsync ( string databaseName )
839
882
{
840
- return Task . Factory . StartNew ( ( ) =>
841
- {
842
- ChangeDatabase ( databaseName ) ;
843
- } ) ;
883
+ return ChangeDataBaseAsync ( databaseName , CancellationToken . None ) ;
844
884
}
885
+
845
886
/// <summary>
846
- /// Async version of Open
887
+ /// Async version of ChangeDataBase
847
888
/// </summary>
889
+ /// <param name="databaseName">The name of the database to use.</param>
848
890
/// <returns></returns>
849
- public Task OpenAsync ( )
891
+ public Task ChangeDataBaseAsync ( string databaseName , CancellationToken cancellationToken )
850
892
{
851
- return Task . Factory . StartNew ( ( ) =>
893
+ var result = new TaskCompletionSource < bool > ( ) ;
894
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
852
895
{
853
- Open ( ) ;
854
- } ) ;
896
+ try
897
+ {
898
+ ChangeDatabase ( databaseName ) ;
899
+ result . SetResult ( true ) ;
900
+ }
901
+ catch ( Exception ex )
902
+ {
903
+ result . SetException ( ex ) ;
904
+ }
905
+ }
906
+ return result . Task ;
855
907
}
908
+
909
+ ///// <summary>
910
+ ///// Async version of Open
911
+ ///// </summary>
912
+ ///// <returns></returns>
913
+ //public Task OpenAsync()
914
+ //{
915
+ // return Task.Run(() =>
916
+ // {
917
+ // Open();
918
+ // });
919
+ //}
920
+
856
921
/// <summary>
857
922
/// Async version of Close
858
923
/// </summary>
859
924
/// <returns></returns>
860
925
public Task CloseAsync ( )
861
926
{
862
- return Task . Factory . StartNew ( ( ) =>
927
+ return CloseAsync ( CancellationToken . None ) ;
928
+ }
929
+
930
+ public Task CloseAsync ( CancellationToken cancellationToken )
931
+ {
932
+ var result = new TaskCompletionSource < bool > ( ) ;
933
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
863
934
{
864
- Close ( ) ;
865
- } ) ;
935
+ try
936
+ {
937
+ Close ( ) ;
938
+ result . SetResult ( true ) ;
939
+ }
940
+ catch ( Exception ex )
941
+ {
942
+ result . SetException ( ex ) ;
943
+ }
944
+ }
945
+ else
946
+ {
947
+ result . SetCanceled ( ) ;
948
+ }
949
+ return result . Task ;
866
950
}
951
+
867
952
/// <summary>
868
953
/// Async version of ClearPool
869
954
/// </summary>
870
955
/// <param name="connection">The connection associated with the pool to be cleared.</param>
871
956
/// <returns></returns>
872
957
public Task ClearPoolAsync ( MySqlConnection connection )
873
958
{
874
- return Task . Factory . StartNew ( ( ) =>
959
+ return ClearPoolAsync ( connection , CancellationToken . None ) ;
960
+ }
961
+
962
+ public Task ClearPoolAsync ( MySqlConnection connection , CancellationToken cancellationToken )
963
+ {
964
+ var result = new TaskCompletionSource < bool > ( ) ;
965
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
875
966
{
876
- ClearPool ( connection ) ;
877
- } ) ;
967
+ try
968
+ {
969
+ ClearPool ( connection ) ;
970
+ result . SetResult ( true ) ;
971
+ }
972
+ catch ( Exception ex )
973
+ {
974
+ result . SetException ( ex ) ;
975
+ }
976
+ }
977
+ else
978
+ {
979
+ result . SetCanceled ( ) ;
980
+ }
981
+ return result . Task ;
878
982
}
983
+
879
984
/// <summary>
880
985
/// Async version of ClearAllPools
881
986
/// </summary>
882
987
/// <returns></returns>
883
988
public Task ClearAllPoolsAsync ( )
884
989
{
885
- return Task . Factory . StartNew ( ( ) =>
990
+ return ClearAllPoolsAsync ( CancellationToken . None ) ;
991
+ }
992
+
993
+ public Task ClearAllPoolsAsync ( CancellationToken cancellationToken )
994
+ {
995
+ var result = new TaskCompletionSource < bool > ( ) ;
996
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
997
+ {
998
+ try
999
+ {
1000
+ ClearAllPools ( ) ;
1001
+ result . SetResult ( true ) ;
1002
+ }
1003
+ catch ( Exception ex )
1004
+ {
1005
+ result . SetException ( ex ) ;
1006
+ }
1007
+ }
1008
+ else
886
1009
{
887
- ClearAllPools ( ) ;
888
- } ) ;
1010
+ result . SetCanceled ( ) ;
1011
+ }
1012
+ return result . Task ;
889
1013
}
890
1014
/// <summary>
891
1015
/// Async version of GetSchemaCollection
@@ -895,10 +1019,29 @@ public Task ClearAllPoolsAsync()
895
1019
/// <returns>A schema collection</returns>
896
1020
public Task < MySqlSchemaCollection > GetSchemaCollectionAsync ( string collectionName , string [ ] restrictionValues )
897
1021
{
898
- return Task . Factory . StartNew ( ( ) =>
1022
+ return GetSchemaCollectionAsync ( collectionName , restrictionValues , CancellationToken . None ) ;
1023
+ }
1024
+
1025
+ public Task < MySqlSchemaCollection > GetSchemaCollectionAsync ( string collectionName , string [ ] restrictionValues , CancellationToken cancellationToken )
1026
+ {
1027
+ var result = new TaskCompletionSource < MySqlSchemaCollection > ( ) ;
1028
+ if ( cancellationToken == CancellationToken . None || ! cancellationToken . IsCancellationRequested )
899
1029
{
900
- return GetSchemaCollection ( collectionName , restrictionValues ) ;
901
- } ) ;
1030
+ try
1031
+ {
1032
+ var schema = GetSchemaCollection ( collectionName , restrictionValues ) ;
1033
+ result . SetResult ( schema ) ;
1034
+ }
1035
+ catch ( Exception ex )
1036
+ {
1037
+ result . SetException ( ex ) ;
1038
+ }
1039
+ }
1040
+ else
1041
+ {
1042
+ result . SetCanceled ( ) ;
1043
+ }
1044
+ return result . Task ;
902
1045
}
903
1046
#endregion
904
1047
#endif
0 commit comments