Package net.sourceforge.jtds.jdbc
Class SharedSocket
java.lang.Object
net.sourceforge.jtds.jdbc.SharedSocket
- Direct Known Subclasses:
SharedLocalNamedPipe
,SharedNamedPipe
This class manages the physical connection to the SQL Server and
serialises its use amongst a number of virtual sockets.
This allows one physical connection to service a number of concurrent
statements.
Constraints and assumptions:
- Callers will not attempt to read from the server without issuing a request first.
- The end of a server reply can be identified as byte 2 of the header is non zero.
- This code will discard unread server data if a new request is issued. Currently the higher levels of the driver attempt to do this but may be we can just rely on this code instead.
- A cancel can be issued by a caller only if the server is currently sending data for the caller otherwise the cancel is ignored.
- Cancel packets on their own are returned as extra records appended to the previous packet so that the TdsCore module can process them.
- Author:
- Mike Hutchinson, Holger Rehn
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
This inner class contains the state information for the virtual socket. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicInteger
last ID assigned to a VirtualSocket instanceprivate final ConcurrentMap<Integer,
SharedSocket.VirtualSocket> Table of stream objects sharing this socket.private final File
The directory to buffer data to.private final Object
Synchronization monitor forcancelPending
andresponseOwner
.private boolean
A cancel packet is pending.private CharsetInfo
The character set to use for converting strings to/from bytes.private final byte[]
Buffer for TDS_DONE packetsprivate int
How much of the doneBuffer has been filled with data, <TDS_DONE_LEN IFF partial packet read.private static int
Total memory usage in all instances of the driver NB.private final byte[]
Buffer for packet header.private String
The server host name.private DataInputStream
Input stream for network socket.private int
Current maximum input buffer size.private static int
Max memory limit to use for buffers.private static int
Minimum number of packets that will be cached in memory before the driver tries to write to disk even if memoryBudget has been exceeded.private DataOutputStream
Output stream for network socket.private int
Count of packets received.private static int
Peak memory usage for debug purposes.private int
The server port number.private SharedSocket.VirtualSocket
The virtual socket of the object that is expecting a response from the server.private static boolean
Global flag to indicate that security constraints mean that attempts to create work files will fail.protected final int
The servertype one of Driver.SQLSERVER or Driver.SYBASEprivate Socket
The shared network socket.private Socket
The shared SSL network socket;private static final int
Length of a TDS_DONE token.private static final int
TDS done token.private static final int
Length of TDS packet header.private int
Tds protocol version -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SharedSocket
(File bufferDir, int tdsVersion, int serverType) (package private)
SharedSocket
(JtdsConnection connection) Construct aSharedSocket
object specifying host name and port. -
Method Summary
Modifier and TypeMethodDescription(package private) boolean
cancel
(SharedSocket.VirtualSocket vsock) Send a TDS cancel packet to the server.(package private) void
close()
Close the socket and release all resources.(package private) void
Deallocate a stream linked to this socket.private Socket
createSocketForJDBC3
(JtdsConnection connection) Creates aSocket
connection.private byte[]
Read a cached packet from the in memory queue or from a disk based queue.(package private) void
Disable TLS encryption and switch back to raw TCP/IP socket.(package private) void
enableEncryption
(String ssl) Enable TLS encryption by creating a TLS socket over the existing TCP/IP network socket.private void
enqueueInput
(SharedSocket.VirtualSocket vsock, byte[] buffer) Save a packet buffer in a memory queue or to a disk queue if the global memory limit for the driver has been exceeded.protected void
finalize()
Ensure all resources are released.(package private) void
Force close the socket causing any pending reads/writes to fail.(package private) String
Retrieve the character set name used to translate byte arrays to or from Strings.(package private) CharsetInfo
Retrieve the character set descriptor used to translate byte arrays to or from Strings.protected String
getHost()
Get the server host name.protected DataInputStream
getIn()
Getter forin
field.(package private) String
getMAC()
(package private) static int
Get the global buffer memory limit for all instancs of this driver.(package private) static int
Get the minimum number of memory cached packets.(package private) byte[]
getNetPacket
(SharedSocket.VirtualSocket vsock, byte[] buffer) Get a network packet.protected DataOutputStream
getOut()
Getter forout
field.(package private) static int
getPktLen
(byte[] buf) Convert two bytes (in network byte order) in a byte array into a Java short integer.protected int
getPort()
Get the server port number.(package private) RequestStream
getRequestStream
(int bufferSize, int maxPrecision) Obtain an instance of a server request stream for this socket.(package private) ResponseStream
getResponseStream
(RequestStream requestStream, int bufferSize) Obtain an instance of a server response stream for this socket.(package private) int
Retrieve the TDS version that is active on the connection supported by this socket.(package private) boolean
Get the connected status of this socket.private byte[]
readPacket
(byte[] buffer) Read a physical TDS packet from the network.(package private) byte[]
sendNetPacket
(SharedSocket.VirtualSocket vsock, byte[] buffer) Send a network packet.(package private) void
setCharsetInfo
(CharsetInfo charsetInfo) Set the character set descriptor to be used to translate byte arrays to or from Strings.protected void
setIn
(DataInputStream in) Setter forin
field.protected void
setKeepAlive
(boolean keepAlive) Set the socket keep alive.(package private) static void
setMemoryBudget
(int memoryBudget) Set the global buffer memory limit for all instances of this driver.(package private) static void
setMinMemPkts
(int minMemPkts) Set the minimum number of packets to cache in memory before writing to disk.protected void
setOut
(DataOutputStream out) Setter forout
field.protected void
setTdsVersion
(int tdsVersion) Set the TDS version field.protected void
setTimeout
(int timeout) Set the socket timeout.
-
Field Details
-
socket
The shared network socket. -
sslSocket
The shared SSL network socket; -
out
Output stream for network socket. -
in
Input stream for network socket. -
maxBufSize
private int maxBufSizeCurrent maximum input buffer size. -
_LastID
last ID assigned to a VirtualSocket instance -
_VirtualSockets
Table of stream objects sharing this socket. -
responseOwner
The virtual socket of the object that is expecting a response from the server. -
hdrBuf
private final byte[] hdrBufBuffer for packet header. -
bufferDir
The directory to buffer data to. -
globalMemUsage
private static int globalMemUsageTotal memory usage in all instances of the driver NB. Access to this field should probably be synchronized but in practice lost updates will not matter much and I think all VMs tend to do atomic saves to integer variables. -
peakMemUsage
private static int peakMemUsagePeak memory usage for debug purposes. -
memoryBudget
private static int memoryBudgetMax memory limit to use for buffers. Only when this limit is exceeded will the driver start caching to disk. -
minMemPkts
private static int minMemPktsMinimum number of packets that will be cached in memory before the driver tries to write to disk even if memoryBudget has been exceeded. -
securityViolation
private static boolean securityViolationGlobal flag to indicate that security constraints mean that attempts to create work files will fail. -
tdsVersion
private int tdsVersionTds protocol version -
serverType
protected final int serverTypeThe servertype one of Driver.SQLSERVER or Driver.SYBASE -
charsetInfo
The character set to use for converting strings to/from bytes. -
packetCount
private int packetCountCount of packets received. -
host
The server host name. -
port
private int portThe server port number. -
cancelPending
private boolean cancelPendingA cancel packet is pending. -
cancelMonitor
Synchronization monitor forcancelPending
andresponseOwner
. -
doneBuffer
private final byte[] doneBufferBuffer for TDS_DONE packets -
doneBufferFrag
private int doneBufferFragHow much of the doneBuffer has been filled with data, <TDS_DONE_LEN IFF partial packet read. -
TDS_DONE_TOKEN
private static final int TDS_DONE_TOKENTDS done token.- See Also:
-
TDS_DONE_LEN
private static final int TDS_DONE_LENLength of a TDS_DONE token.- See Also:
-
TDS_HDR_LEN
private static final int TDS_HDR_LENLength of TDS packet header.- See Also:
-
-
Constructor Details
-
SharedSocket
-
SharedSocket
SharedSocket(JtdsConnection connection) throws IOException, UnknownHostException Construct aSharedSocket
object specifying host name and port.- Parameters:
connection
- the connection object- Throws:
IOException
- if socket open failsUnknownHostException
-
-
Method Details
-
createSocketForJDBC3
Creates aSocket
connection.- Parameters:
connection
- the connection object- Returns:
- a socket open to the host and port with the given timeout
- Throws:
IOException
- if socket open fails
-
getMAC
String getMAC() -
enableEncryption
Enable TLS encryption by creating a TLS socket over the existing TCP/IP network socket.- Parameters:
ssl
- the SSL URL property value- Throws:
IOException
- if an I/O error occurs
-
disableEncryption
Disable TLS encryption and switch back to raw TCP/IP socket.- Throws:
IOException
- if an I/O error occurs
-
setCharsetInfo
Set the character set descriptor to be used to translate byte arrays to or from Strings.- Parameters:
charsetInfo
- the character set descriptor
-
getCharsetInfo
CharsetInfo getCharsetInfo()Retrieve the character set descriptor used to translate byte arrays to or from Strings. -
getCharset
String getCharset()Retrieve the character set name used to translate byte arrays to or from Strings.- Returns:
- the character set name as a
String
-
getRequestStream
Obtain an instance of a server request stream for this socket.- Parameters:
bufferSize
- the initial buffer size to be used by theRequestStream
maxPrecision
- the maximum precision for numeric/decimal types- Returns:
- the server request stream as a
RequestStream
-
getResponseStream
Obtain an instance of a server response stream for this socket. NB. getRequestStream() must be used first to obtain the RequestStream needed as a parameter for this method.- Parameters:
requestStream
- an existing server request stream object obtained from thisSharedSocket
bufferSize
- the initial buffer size to be used by theRequestStream
- Returns:
- the server response stream as a
ResponseStream
-
getTdsVersion
int getTdsVersion()Retrieve the TDS version that is active on the connection supported by this socket.- Returns:
- the TDS version as an
int
-
setTdsVersion
protected void setTdsVersion(int tdsVersion) Set the TDS version field.- Parameters:
tdsVersion
- the TDS version as anint
-
setMemoryBudget
static void setMemoryBudget(int memoryBudget) Set the global buffer memory limit for all instances of this driver.- Parameters:
memoryBudget
- the global memory budget
-
getMemoryBudget
static int getMemoryBudget()Get the global buffer memory limit for all instancs of this driver.- Returns:
- the memory limit as an
int
-
setMinMemPkts
static void setMinMemPkts(int minMemPkts) Set the minimum number of packets to cache in memory before writing to disk.- Parameters:
minMemPkts
- the minimum number of packets to cache
-
getMinMemPkts
static int getMinMemPkts()Get the minimum number of memory cached packets.- Returns:
- minimum memory packets as an
int
-
isConnected
boolean isConnected()Get the connected status of this socket.- Returns:
true
if the underlying socket is connected
-
close
Close the socket and release all resources.- Throws:
IOException
- if the socket close fails
-
forceClose
void forceClose()Force close the socket causing any pending reads/writes to fail.Used by the login timer to abort a login attempt.
-
readPacket
Read a physical TDS packet from the network.- Parameters:
buffer
- a buffer to read the data into (if it fits) or null- Returns:
- either the incoming buffer if it was large enough or a newly allocated buffer with the read packet
- Throws:
IOException
-
getPktLen
static int getPktLen(byte[] buf) Convert two bytes (in network byte order) in a byte array into a Java short integer.- Parameters:
buf
- array of data- Returns:
- the 16 bit unsigned value as an
int
-
setTimeout
Set the socket timeout.- Parameters:
timeout
- the timeout value in milliseconds- Throws:
SocketException
-
setKeepAlive
Set the socket keep alive.- Parameters:
keepAlive
-true
to turn on socket keep alive- Throws:
SocketException
-
getIn
Getter forin
field.- Returns:
InputStream
used for communication
-
setIn
Setter forin
field.- Parameters:
in
- theInputStream
to be used for communication
-
getOut
Getter forout
field.- Returns:
OutputStream
used for communication
-
setOut
Setter forout
field.- Parameters:
out
- theOutputStream
to be used for communication
-
getHost
Get the server host name.- Returns:
- the host name as a
String
-
getPort
protected int getPort()Get the server port number.- Returns:
- the host port as an
int
-
finalize
Ensure all resources are released.
-